Reputation: 3137
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
I receive INVALID_REQUEST as a response. But when I try this link in browser or with the help of REST client, everything works fine.
The same with this request :
https://maps.googleapis.com/maps/api/geocode/json?address=Abertamy-Horní+Blatná®ion=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();
}
}
Upvotes: 0
Views: 126
Reputation: 812
Here is an idea: Check the output of your 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.
The solution in this case is wait 1 second if there is a next_page_token and then send the followup request.
Upvotes: 1