I am using google geocoding api to get the results but it is returning “ZERO_RESULTS”. Below is the code
public class GeoCode {
public static void main(String[] args) throws Exception {
GeoCode.geocode("latlng=40.714224,-73.961452");
}
private static final String URL = "http://maps.googleapis.com/maps/api/geocode/json";
public static void geocode(String address) throws Exception {
URL url = new URL(URL + "?" + URLEncoder.encode(address, "UTF-8") + "&sensor=false");
URLConnection conn = url.openConnection();
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
IOUtils.copy(conn.getInputStream(), output);
output.close();
//JSONObject json = new JSONObject(output.toString());
System.out.println(output.toString());
}
}
When I use the address manually on the address bar of the browser it shows me the json result on the browser. But what is actually wrong with the above code, that it is returning “ZERO_RESULTS” in a json?
You need not encode the address with URLEncoder, just do:
or maybe better: