[英]Android : Google API v.3 gives INVALID_REQUEST , but executed good in the browser
I need to geocode the address and get latitude , longitude from the city, postal code and other. 我需要对地址进行地理编码,并从城市,邮政编码等获取纬度,经度。
When I use this request with the Android https://maps.googleapis.com/maps/api/geocode/json?address=Aachen-Horbach+Gzg®ion=DEU&sensor=false 当我将此请求与Android https://maps.googleapis.com/maps/api/geocode/json?address=Aachen-Horbach+Gzg®ion=DEU&sensor=false一起使用时
I receive INVALID_REQUEST as a response. 我收到INVALID_REQUEST作为答复。 But when I try this link in browser or with the help of REST client, everything works fine.
但是,当我在浏览器中或在REST客户端的帮助下尝试此链接时,一切正常。
The same with this request : 与此请求相同:
https://maps.googleapis.com/maps/api/geocode/json?address=Abertamy -Horní+Blatná®ion=CZE&sensor=false https://maps.googleapis.com/maps/api/geocode/json?address=Abertamy-Horní+Blatná&region = CZE&sensor = false
What can be the reason ? 可能是什么原因?
Url creation : 网址创建:
StringBuilder addressUrl = new StringBuilder();
if (order.getDepartureAddress().getStreet()!=null && !order.getDepartureAddress().getStreet().equalsIgnoreCase(""))
addressUrl.append(order.getDepartureAddress().getStreet() + ", ");
if (order.getDepartureAddress().getHouseNumber()!=null && ! order.getDepartureAddress().getHouseNumber().equalsIgnoreCase(""))
addressUrl.append(order.getDepartureAddress().getHouseNumber() + ", ");
if (order.getDepartureAddress().getCity()!=null && !order.getDepartureAddress().getCity().equalsIgnoreCase(""))
addressUrl.append(order.getDepartureAddress().getCity() );
if (order.getDepartureAddress().getCountryCode()!=null && !order.getDepartureAddress().getCountryCode().equalsIgnoreCase(""))
addressUrl.append("®ion="+order.getDepartureAddress().getCountryCode() );
addressUrl.append("&sensor=false");
/*
Constants.URL_GEOCODING = https://maps.googleapis.com/maps/api/geocode/json?address=
*/
String finalUrl = Constants.URL_GEOCODING + addressUrl.toString();
/* request execution : */
public static JSONObject readJsonFromUrl(String urlStr) throws IOException, JSONException {
URL url = new URL(urlStr);
URI uri = null;
URI uri2= null;
try {
uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();
uri2 = new URI(url.toString().replace("%20", "+"));
} catch (URISyntaxException e) {
e.printStackTrace();
}
url = uri2.toURL();
Log.i(TAG + " requested url", url.toString());
InputStream is = url.openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
Here is an idea: Check the output of your logcat. 这是一个主意:检查logcat的输出。
If the request time and the time when the data was sent back from google are too close to each other, Google might assume you are a spammer and deny access to the server on the second request. 如果请求时间和从Google发回数据的时间彼此距离太近,则Google可能会认为您是垃圾邮件发送者,并拒绝在第二次请求时访问服务器。
The solution in this case is wait 1 second if there is a next_page_token and then send the followup request. 在这种情况下,解决方案是如果存在next_page_token,则等待1秒钟,然后发送后续请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.