Question

i would like to make a simple HTTP GET with the PlayFramework (java) but it doesn't work. I've googled a lot an read in the documentation of play 2.x but nothing helped me. The examples in the documentation also did not help.

i would like to write a method which calls the reverse geoconding service (for example this) and gives the JSON data back. I need the response data as JsonNode or something like that.

i tried the following piece of code from the play homepage which should give back the name of a road, but it did not work.

public static Promise<Result> reverseLookup() {//String lat, String lon

    final Promise<Result> resultPromise = WS.url("http://nominatim.openstreetmap.org/reverse?format=json&lat=51.510809&lon=-0.092875").get().map(
            new Function<WS.Response, Result>() {
                public Result apply(WS.Response response) {
                    return ok("Road:" + response.asJson().findPath("road"));
                }
            }
    );
    return resultPromise;
}

I got this error here:

[RuntimeException: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [Source: org.jboss.netty.buffer.ChannelBufferInputStream@6a43dbaf; line: 1, column: 2]]

there isn't any character like "<" in the JSON data if this url is called manually in the browser.

Please help me. I don't get it.


EDIT 10:25 PM:

i tried response.getBody() but the output is the same.

I don't understand where the error could be... Here is more code:

routes:

POST    /other/function         controllers.myController.anotherFunction()

myController:

private static String reverseLookupData;

@BodyParser.Of(play.mvc.BodyParser.Json.class)
public static Result anotherFunction() {
    // the gps coordinates are passed to this method (this works up to this point)
    reverseLookup(String lat, String lon); // reverse lookup starts
    System.out.println("Response 1: " + reverseLookupData);
    System.out.println("Response 2: " + reverseLookup().toString());
    // some code ...
}

public static Promise<String> reverseLookup(String lat, String lon) {

    final Promise<String> resultPromise = WS.url("http://nominatim.openstreetmap.org/reverse?format=json&lat="+ lat +"&lon=" + lon).get().map(
            new Function<WS.Response, String>() {
                public String apply(WS.Response response) {
                    reverseLookupData = response.getBody(); 
                    return "Data:" + reverseLookupData;
                }
            }
    );
    return resultPromise;
}

If i run this code the output to the command-line window is: Response 1: null Response 2: play.libs.F$Promise@51ebebed


EDIT

(...)
public String apply(WS.Response response) {
    System.out.println("Body: " + response.getBody()); 
    return "";
}
(...)

Now i got this error message here:

Body: <?xml version="1.0" encoding="UTF-8" ?>
<reversegeocode timestamp='Sun, 06 Apr 14 21:31:45 +0000' attribution='Data ┬® OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright' querystring=''>
<error>Unable to geocode</error></reversegeocode>

Anybody an idea why this appears or where the error could be? As I said, when I put the geocoding address in the browser, then I get a correct answer.


i tried googles reverse geocoding but that also does not work

url:

"http://maps.googleapis.com/maps/api/geocode/json?latlng=51.510809,-0.092875&sensor=false&key={myServerKey}",
"http://maps.googleapis.com/maps/api/geocode/json?latlng=51.510809,-0.092875&sensor=false"

sensor true or false, nothing works:

Body: {
   "error_message" : "The 'sensor' parameter specified in the request must be set to either 'true' or 'false'.",
   "results" : [],
   "status" : "REQUEST_DENIED"
}

I'm grateful for any help :)

Was it helpful?

Solution

WS.url("http://nominatim.openstreetmap.org/reverse")
  .withQueryParameter("format", "json")
  .withQueryParameter("lat", lat)
  .withQueryParameter("lon", lon)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top