1

am calling a google geocoding api via java, and getting wrong answer ! for example if i run the query below from browser i get the location but if i run it using a rest client i get zero result !!! is there anything to solve this issue ?

https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=18+RUE+DE+NIEDECK++++39255+OTTERSWILLER&region=FR&key=AI.....

this is the answer from Google Chrome or Mozilla :

{ "results" : [ { "address_components" : [ { "long_name" : "18", "short_name" : "18", "types" : [ "street_number" ] }, { "long_name" : "Rue du Nideck", "short_name" : "Rue du Nideck", "types" : [ "route" ] }, { "long_name" : "Otterswiller", "short_name" : "Otterswiller", "types" : [ "locality", "political" ] }, { "long_name" : "Bas-Rhin", "short_name" : "Bas-Rhin", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Grand Est", "short_name" : "Grand Est", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "France", "short_name" : "FR", "types" : [ "country", "political" ] }, { "long_name" : "67700", "short_name" : "67700", "types" : [ "postal_code" ] } ], "formatted_address" : "18 Rue du Nideck, 67700 Otterswiller, France", "geometry" : { "location" : { "lat" : 48.7237784, "lng" : 7.363452799999998 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 48.7251273802915, "lng" : 7.364801780291501 }, "southwest" : { "lat" : 48.7224294197085, "lng" : 7.362103819708497 } } }, "partial_match" : true, "place_id" : "ChIJXwysfE2nlkcRjwlaKN_gmrE", "types" : [ "street_address" ] } ], "status" : "OK" }

and this is the answer for the same address when i use a rest client :

{ "results" : [], "status" : "ZERO_RESULTS" }

This is my code , and i am using apache http client version 4.5

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>

as for the code here it is :

        String f = "https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=18+RUE+DE+NIEDECK++++39255+OTTERSWILLER&region=FR&key=googlekey";

    // executing the request
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(f);
    HttpResponse response = client.execute(request);
    String responseBody = EntityUtils.toString(response.getEntity());
    System.out.println(responseBody);
2
  • What REST client do you use? This is most likely an implementation issue caused by a clientside issue. Even better: please post the code snippet of how you launch the API request.
    – Senshi
    Commented Apr 13, 2017 at 8:07
  • am using both RESTClient and ARC , this is the call i make from my java client : String f = "https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=18+RUE+DE+NIEDECK++++39255+OTTERSWILLER&region=FR&key=AI"; HttpGet httpGet = new HttpGet(f); HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(f); HttpResponse response = client.execute(request); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println(responseBody);
    – Zeus
    Commented Apr 13, 2017 at 9:45

1 Answer 1

2

Given your comment, I think I found your problem:

Calling

https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=18+RUE+DE+NIEDECK++++39255+OTTERSWILLER&region=FR

as in your question works. Calling

https://maps.googleapis.com/maps/api/geocode/json?sensor=fa‌​lse&address=18+RUE+D‌​E+NIEDECK++++39255+O‌​TTERSWILLER&region=F‌​R

as in your code example from the comment does not work. Using the syntax highlighting here, you already see that there's an issue in the URL. I don't know why, but you managed to introduce two zero-width spaces between the a and the l of sensor=false.

Remove those, and your code will most likely work. To prevent this happening in the future, look into sanitizing URLs that come from unreliable sources (anything not under your control, e.g. user input or dynamic from other sources).

EDIT: Another likely issue: you don't set any request headers at all. The google API is not a fan of that.

The essential header you are missing is probably accept-language . It should look something like this:

accept-language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4

I don't have Java set up here, but adding

request.addHeader("accept-language", "de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4");

before you client.execute(request) should do the trick. Obviously you can adapt the values to your liking, in this case I used my (German) browser's default values.

With that header set, the request also passes ARC.

5
  • Sorry to disappoint you but it doesn't work i even remover the sensor parameter but i still get the no result answer from both restclinet and my code , given that i also re-encoded my url with %20 for spaces and it does not work neither till now i didn't figure out what the problem is and why i get what i get ! Issue Stil not solved yet :/
    – Zeus
    Commented Apr 13, 2017 at 10:07
  • also i did retyped the query my self by hand with no spaces but no luck !
    – Zeus
    Commented Apr 13, 2017 at 10:10
  • Then please edit your question, providing the necessary example code as well as the libraries+ import statements you use. Even better, set up an example yourself using tutorialspoint.com/codingground.htm so we can properly analyze. It is 99,9% certain that the problem lies in your code, and not the Google API, because as you noticed, the request works just fine on its own.
    – Senshi
    Commented Apr 13, 2017 at 10:21
  • Done, you can see the code in the edited question
    – Zeus
    Commented Apr 13, 2017 at 10:41
  • Updated my answer, identified a missing header.
    – Senshi
    Commented Apr 13, 2017 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.