- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在我的 Android 应用程序中,用户可以选择他想查看的地点的类别(咖啡馆、餐馆等)。
当我使用以下 url 时,我从带有 next_page_token 的 google-places-api 获得结果:
https://maps.googleapis.com/maps/api/place/textsearch/json?query=cafe+in+Elfde-Liniestraat%2C+Hasselt%2C+Belgium&pagetoken=ClRHAAAAqYa7IpwMQH3q1DyinfsMnkuOaZg8wQrp0pvzarv13pwqruqT2YfaEry6v_d-L4d_TC13G6iHuPz-rc1KbI6XRE01rB5CuXcHISu-CI_CaKESEMUxSjHhHDNee4xbr2tPEZcaFPvO6xlRDjs-5AixyD9vamWwTGlU&sensor=true&key=<mykey>
或这个网址:
https://maps.googleapis.com/maps/api/place/textsearch/json?pagetoken=ClRHAAAAqYa7IpwMQH3q1DyinfsMnkuOaZg8wQrp0pvzarv13pwqruqT2YfaEry6v_d-L4d_TC13G6iHuPz-rc1KbI6XRE01rB5CuXcHISu-CI_CaKESEMUxSjHhHDNee4xbr2tPEZcaFPvO6xlRDjs-5AixyD9vamWwTGlU&sensor=true&key=<mykey>
我收到一个 INVALID_REQUEST。
这是我用来运行和获取结果的代码:
@Override
protected ArrayList<String> doInBackground(String... params) {
// When more then one category is selected, this character needs to be after it.
final String DELIMITINGCHARACTER = "|";
// Add all the categories to a variable to put later on in the URL.
String myTypes = "";
for (String category : params){
// A category must be lowercase letters only.
category = category.toLowerCase();
// A category may not display a whitespace.
// The whitespace(s) must be replaced with a '_'.
category = category.replaceAll(" ", "_");
myTypes += category + DELIMITINGCHARACTER;
}
// Delete if the string ends with the delimitingcharachter the delimitingcharacter!
if (myTypes.endsWith("|")){
myTypes = myTypes.substring(0, myTypes.length() - 1);
}
ArrayList<String> myPointsOfInterest = new ArrayList<String>();
try{
String urlString;
String urlData;
//String urlApi = "&sensor=true&key=<myKey>"; // Key 1
String urlApi = "&sensor=true&key=<myKey>"; // Key 2
if (_txtFindLocation.getText().length() > 0){
urlString = "maps.googleapis.com/maps/api/place/textsearch/json?";
urlData = "query=" + URLEncoder.encode(myTypes, "UTF-8") + "+in+"
+ URLEncoder.encode(_txtFindLocation.getText().toString(), "UTF-8");
}
else{
urlString = "maps.googleapis.com/maps/api/place/search/json?";
urlData = "location=" + _myCurrentPosition.getLatitudeE6() / 1e6 + "," + _myCurrentPosition.getLongitudeE6() / 1e6
+ "&radius=5000&types=" + URLEncoder.encode(myTypes, "UTF-8");
}
ArrayList<String> myJsonString = new ArrayList<String>();
myJsonString.add(getDataOfUrl(urlString, urlData, urlApi, null));
for (String jsonString : myJsonString){
// Turn String into a JSON Object.
JSONObject jsonPredictionsObject = new JSONObject(jsonString);
String urlToken = jsonPredictionsObject.optString("next_page_token");
if (urlToken != ""){
myJsonString.add(getDataOfUrl(urlString, urlData, urlApi, "&pagetoken=" + urlToken));
}
}
Log.d("MainActivity", "size of myJsonString " + String.valueOf(myJsonString.size()));
for (String jsonString : myJsonString){
Log.d("MainActivity", jsonString);
// Turn String into a JSON Object.
JSONObject jsonPredictionsObject = new JSONObject(jsonString);
// Get a JSON Array that is inside the JSON Object.
JSONArray jsonPredictionsArray = jsonPredictionsObject.getJSONArray("results");
for (int i = 0; i < jsonPredictionsArray.length(); i++){
jsonPredictionsObject = (JSONObject) jsonPredictionsArray.get(i);
final String name = jsonPredictionsObject.getString("name");
final String vicinity = jsonPredictionsObject.optString("vicinity").compareTo("") != 0 ? jsonPredictionsObject.optString("vicinity") : jsonPredictionsObject.optString("formatted_address");
final JSONObject geometryObject = jsonPredictionsObject.getJSONObject("geometry");
final JSONObject locationObject = geometryObject.getJSONObject("location");
final JSONArray typesArray = jsonPredictionsObject.getJSONArray("types");
final double latitude = locationObject.getDouble("lat");
final double longitude = locationObject.getDouble("lng");
String types = "";
for (int t = 0; t < typesArray.length(); t++){
String type = typesArray.getString(t);
type = type.toLowerCase();
type = type.replaceAll("_", " ");
types += type + " - ";
}
types = types.substring(0, types.length() - 2);
myPointsOfInterest.add(String.valueOf(latitude) + DELIMITINGCHARACTER +
String.valueOf(longitude) + DELIMITINGCHARACTER + types +
DELIMITINGCHARACTER + name + DELIMITINGCHARACTER + vicinity);
}
}
}
catch (IOException e){
Log.e("Search points of interest", "GetPointsOfinterest: doInBackGround", e);
}
catch (JSONException e){
Log.e("Search points of interest", "GetPointsOfinterest: doInBackGround", e);
}
return myPointsOfInterest;
}
public String getDataOfUrl(String urlString, String urlData, String urlApi, String urlToken){
try{
URL myURL;
if (urlToken == null) {
myURL = new URL("https://" + urlString + urlData + urlApi);
}
else {
Log.d("URL INPUT", "https://" + urlString + urlData + urlToken + urlApi);
myURL = new URL("https://" + urlString + urlData + urlToken + urlApi);
}
URLConnection myURLConnection = myURL.openConnection();
BufferedReader myBufferReader = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
String strLine;
StringBuffer strBuffer = new StringBuffer();
// Take Google's legible JSON and turn it into on big String.
while((strLine = myBufferReader.readLine()) != null){
strBuffer.append(strLine);
}
Log.d("URL OUTPUT", strBuffer.toString());
return strBuffer.toString();
}
catch (IOException e){
Log.e("getDataOfUrl", "Error: ", e);
}
return null;
}
这是 logcat 的输出:
11-03 14:47:13.832: D/URL OUTPUT(19532): { "html_attributions" : [], "next_page_token" : "ClRHAAAAqYa7IpwMQH3q1DyinfsMnkuOaZg8wQrp0pvzarv13pwqruqT2YfaEry6v_d-L4d_TC13G6iHuPz-rc1KbI6XRE01rB5CuXcHISu-CI_CaKESEMUxSjHhHDNee4xbr2tPEZcaFPvO6xlRDjs-5AixyD9vamWwTGlU", "results" : [ { "formatted_address" : "Meldertstraat 42, Hasselt, België", "geometry" : { "location" : { "lat" : 50.9311660, "lng" : 5.3423240 } }, "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png", "id" : "e6ba73e5b439dddb83aaefc94c71954da0cb0268", "name" : "Café Café", "rating" : 4.40, "reference" : "CnRpAAAAdVZpUI4NKwsAiQTOjbYG9psNbU7awZkS6_8Ee4r3AvCKhpqWeiDqdof7zYmoUZWKj0A8rOwtncSa1YFVNNo6RxFmmHvuNdWCaucTqkuSSDw8E2o0pf5E6EiMj-7-Hd5xBzDtC0j9d5lLcVWW3AQy1xIQRwgfWwrZkyK65blCeGqhFhoUB9kTW5QVD9sNNuF75MvVewYTcxA", "types" : [ "cafe", "food", "establishment" ] }, { "formatted_address" : "Kempischesteenweg 27, Hasselt, België", "geometry" : { "location" : { "lat" : 50.9350770, "lng" : 5.3397760 } }, "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png", "id" : "dc66d311b3c4f5d1a87d5959dfdbc7616ad10b24", "name" : "Oberonn", "opening_hours" : { "open_now" : true }, "reference" : "CnRlAAAA5q-rz_aOTWsFGQIJz_6zyLbXIczXOTq4PVFxniOjxsCwnw7uIfY8tu7Jk-QbJ-arMWgdClnSpX28q2SqxD8021LGWcaf4Sgo9MaXWpoD6c4TSagyVO9_l_HUXgMoOFJzhHG_NH6z-t1DPxT1zD19PhIQ2gBP9cLazKeB96ZDTpFdsxoUVGB2Ddhrq6JUoTJgrHGL-SINOWk", "types" : [ "cafe", "food", "establishment" ] }, { "formatted_address" : "Willekensmolenstraat 41, Hasselt, België", "geometry" : { "location" : { "lat" : 50.9316830, "lng" : 5.3470750 } }, "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png", "id" : "04937541c2bf71ebec7ac13d506dbbd2727e1ce3", "name" : "Den Drossaerd", "reference" : "CnRqAAAA0cPmuMx0_J4x-o-yn0oT1D3kD-IHYAQPotAPPeDD3DWh2FXkD89kCsYoUGDLBlH2mKY6ju18JyCHvc4W_izLcd6TUOIoXrbUCyrtxOL4sTAFb41kH1aFT4dOBgvigYrGIcV8Qbzq-K4YGL3t7pnf1hIQKmUp-LLqtGHb4waYi2nq8hoU3VgwykpytoSUUPe2Ue1HmPrMRtE", "types" : [ "cafe", "food", "establishment" ] }, { "formatted_address" : "Leopoldplein 1, Hasselt, België", "geometry" : { "location" : { "lat" : 50.9275810, "lng" : 5.3363830 } }, "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png", "id" : "cc30d6f4e6b0db1ed6dfd17bac118bbf78f194ae", "name" : "Century (Hotel)", "rating" : 3.80, "reference" : "CnRsAAAAfw4ucOBllDjMCtRU9epYQelpKHRx3u8JR8jr4I5jFzJsktNn3Un-3LjKKWtXCk3Unofi_6RhPFw9Tp8VU_Z7i-WOa00P-pirtCDjQPxg7sp470Mgh6_6PQspGG39H3k-VIMuUz9HWmoP3sPjump6SRIQTp1LZghfi-_HVaMfQArGPxoU-6ogK2IGMiueMEXP9mFiHlGJmo8", "types" : [ "lodging", "establishment" ] }, { "formatted_address" : "Maastrichterstraat 30, Hasselt, België", "geometry" : { "location" : { "lat" : 50.9295410, "lng" : 5.3391230 } }, "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png", "id" : "352f9da4642588aba419353487b210baeb30ddd3", "name" : "In De Kleine Hal", "reference" : "CnRuAAAAAlnMpdiL4Kluiw1T0-QgKWFv9wwWvrSY4w-B7SLI7gvpqT0FYaMTl6DhxncPLqRyxOTWxw3DP3r1XvNHDKXnAb3eRM-gZYwIqbvzVq62zEZgGw0usQgf48mAWOrDpegdNLnEFRYkIduRwlDMDyIOyBIQlDs9BozH4m_gE-dZKZf7yxoUqw0H_HT_IQnLaVm0YwG1QV7o3kg", "types" : [ "cafe", "food", "establishment" ] }, { "formatted_address" : "Fruitmarkt 32, Hasselt, België", "geometry" : { "location" : { "lat" : 50.9299760, "lng" : 5.3393550 } }, "icon" : "http://maps.gstatic.com/mapfiles
11-03 14:47:13.864: D/URL INPUT(19532): https://maps.googleapis.com/maps/api/place/textsearch/json?query=cafe+in+Elfde-Liniestraat%2C+Hasselt%2C+Belgium&pagetoken=ClRHAAAAqYa7IpwMQH3q1DyinfsMnkuOaZg8wQrp0pvzarv13pwqruqT2YfaEry6v_d-L4d_TC13G6iHuPz-rc1KbI6XRE01rB5CuXcHISu-CI_CaKESEMUxSjHhHDNee4xbr2tPEZcaFPvO6xlRDjs-5AixyD9vamWwTGlU&sensor=true&key=<myKey>
11-03 14:47:13.989: D/URL OUTPUT(19532): { "html_attributions" : [], "results" : [], "status" : "INVALID_REQUEST"}
PS:当我通过浏览器中的代码(使用 next_page_token)时,两种方式都是正确的,只有我的 Android 应用程序无法获得有效数据。是的,我有一个 API key ,但我在这里从代码和我的 logcat 输出中删除了它。
有人知道我在这里做错了什么吗?
编辑当我在 Android 中硬编码我的 url(使用下一页 token )时,后记让它运行。我得到了预期的数据,因此没有返回 Invalid_Request。问题在于在 Android 中构建 url 或 URL 类的使用。
最佳答案
在我自己的问题上找到了答案:一段时间后,我只需要更好地检查我的 logcat 输出。
请求的时间和数据从谷歌发回的时间。
请求1的输出时间:14:47:13:832
请求2的输入时间:14:47:13:864
请求2的输出时间:14:47:13:989
这些时间彼此接近,在我看来,谷歌认为我是垃圾邮件发送者/黑客之类的,并在第二次请求时拒绝我访问服务器。
我的解决方案是如果有 next_page_token 等待 1 秒,然后发送后续请求。
关于android - URL 类呈现 - Invalid_Request with next_page_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13209882/
假设我拥有域 mydomain.com,并且我在服务器上有一个 Web 应用程序,网址为 http://99.99.99.99:1234/MyApplication/startpage.somethi
我正在尝试通过以下方式更新已解析的 URL: u, _ := url.Parse(s) if u.Scheme == "" { u.Scheme = "https" } if u.Path =
如何将 www.somesite.com/api(.*) 映射到 www.somesite.com/$1:9000? (我需要将/api 映射到运行 @ 端口 9000 的 Play 框架应用程序)
我有一个资源结构,如航类 > 座位 > 预订,所以预订属于某个航类的某个座位: http://example.com/jdf_3prGPS4/1/jMBDy46PbNc
我想知道以下网址是否有效。 路径中的点,在主机之后: http://www.example.com/v.b.w..com 主机中的点,作为子域的一部分: http://v.b.w..co.manufa
我有两个域 - crmpicco.co.uk 和 ayrshireminis.com - 如果我浏览到: www.crmpicco.co.uk/mini/new我希望能够重定向到 www.ayrshi
我正在尝试使用 URL 重写和应用程序请求路由来重写到外部 URL。我设置了以下规则: 在规则中,“patternToMatch”是我试
我已经安装了带有 SharePoint 和 Url Rewrite 模块的 IIS 7.0。 是以下句子还是我配置错误才能看到这个结果? Url Redirect 可以将 url 重定向到任何内部(在
我想知道,为了获得良好的 SEO,您必须在 URL 中使用自然语言。您知道字符中单词或短语的最大大小吗?例如: www.me.com/this-is-a-really-long-url.htm 我问这
有人知道在 SEO 友好 URL 中使用逗号有什么问题吗?我正在使用一些在其 SEO 友好 URL 中使用大量逗号的软件;但我 100% 肯定我见过一些程序/平台无法正确识别 URL 并在第一个逗号后
我有一个网站,我正在为所有链接使用干净的 URL。我想知道对于简短的基本 URL 与较长的描述性 URL 有何看法。 例如,如果我的网站是关于 Georgia Bulldog 足球新闻的,那么哪个网站
我正在编写一个类似于 tinyurl 的 URL 缩短器,我想知道如何跟踪已经使用我的服务缩短的 URL?例如,tinyurl 为相同的长 URL 生成相同的小 URL,而不管是谁创建的。如
我是 magento 的新手。我正在开发一个模块。为此,我有一些要显示链接的 css 和 js 文件。我目前有类似 的链接 getSkinUrl('module_tryouts/css/jquery.
我想基于 HTTP_URL 重写 URL 以重定向到不同的端口,同时保留其余的 URL 和查询字符串(如果指定)。例如, http://host/john/page.aspx 应该重定向到 http:
我遇到了以下问题: 我的 Grails (2.2.0) 应用程序具有以下 URL 映射: "/api/clientQuote/$labcode/$cliCode/$quoCode"(controlle
我有一个很长的 URL,它不适合 URL 字段。它一直在修剪。该怎么办?有没有办法增加 SharePoint 2010 中的 URL 字段字符限制? 或者解决方法来容纳长 URL。例如,以下 URL
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
我们从客户以前的开发人员那里继承了相当多的 Google Apps 脚本项目。 Apps 脚本通过嵌入式小部件部署在 Google 网站 (sites.google.com) 的各个页面上。每当我们需
我正在编写一些文档,但遇到了一些词汇问题: http://www.example.com/en/public/img/logo.gif 被称为“绝对”网址,对吗? ../../public/img/l
我们从客户以前的开发人员那里继承了相当多的 Google Apps 脚本项目。 Apps 脚本通过嵌入式小部件部署在 Google 网站 (sites.google.com) 的各个页面上。每当我们需
我是一名优秀的程序员,十分优秀!