2

Hey guys im trying to get a response from this service (http://ip-api.com) that gives you latitude and longitude based on an ip:

So when you pass the ip 55.130.54.69 it returns the following json:

{
    "query": "55.130.54.69",
    "status": "success",
    "continent": "North America",
    "continentCode": "NA",
    "country": "United States",
    "countryCode": "US",
    "region": "AZ",
    "regionName": "Arizona",
    "city": "Sierra Vista",
    "district": "Fort Huachuca",
    "zip": "85613",
    "lat": 31.5552,
    "lon": -110.35,
    "timezone": "America/Phoenix",
    "currency": "USD",
    "isp": "CONUS-RCAS",
    "org": "USAISC",
    "as": "AS721 DoD Network Information Center",
    "asname": "DNIC-ASBLK-00721-00726",
    "mobile": false,
    "proxy": false
}

http://ip-api.com/#55.130.54.69

So in my service im doing the following (I guided with this Best way to get geo-location in Java):

    @POST
    @Path("/test2")
    public void test2(@Context HttpServletRequest request) {

        String ip = request.getRemoteAddr();
        System.out.println("ip: " + ip);
        //Im changing value of ip cause I have an issue with "private range" ip of my machine
        ip = "55.130.54.69";
        // This is working
        Client client = ClientBuilder.newClient();
        Response response = client.target("http://ip-api.com/json/" + ip).request(MediaType.TEXT_PLAIN_TYPE)
                .header("Accept", "application/json").get();

        System.out.println("status: " + response.getStatus()); // Printing 200 so it worked
        System.out.println("body:" + response.getEntity());
        System.out.println("metadata: " + response.getMetadata());
        System.out.println(response);
    }

So as you can see im trying to get that json above in my question but I don't know how, can you show me the way please?

BugsForBreakfast
  • 712
  • 10
  • 30

2 Answers2

2

If you need to get json as a plain text, you can try the next:

@POST
@Path("/test2")
public void test2(@Context HttpServletRequest request) {

    ...

    Response response = client.target("http://ip-api.com/json/" + ip)
        .request(MediaType.TEXT_PLAIN_TYPE)
        .header("Accept", "application/json").get();

   String json = response.readEntity(String.class);
   response.close();

   // now you can do with json whatever you want to do
}

Also you can create an entity class where field names match value names in json:

public class Geolocation {
    private String query;
    private String status;
    private String continent;

    // ... rest of fields and their getters and setters      
}

Then you can read data as an instance of the entity:

@POST
@Path("/test2")
public void test2(@Context HttpServletRequest request) {

    ...

    Response response = client.target("http://ip-api.com/json/" + ip)
        .request(MediaType.TEXT_PLAIN_TYPE)
        .header("Accept", "application/json").get();

   Geolocation location = response.readEntity(Geolocation.class);
   response.close();

   // now the instance of Geolocation contains all data from the message
}

If you're not interested in getting details of response you cant get result message right from the get() method:

Geolocation location = client.target("http://ip-api.com/json/" + ip)
    .request(MediaType.TEXT_PLAIN_TYPE)
    .header("Accept", "application/json").get(Geolocation.class);

// just the same has to work for String
Ken Bekov
  • 13,696
  • 3
  • 36
  • 44
1

What does this print? System.out.println("body:" + response.getEntity()); plus, which libraries are you using to post?, is that jersey?

  • It prints body:org.jboss.resteasy.client.jaxrs.internal.ClientResponse$InputStreamWrapper@160a4908 – BugsForBreakfast Jul 03 '19 at 18:07
  • 3
    try this: `BufferedReader in = new BufferedReader( new InputStreamReader(response.getEntity())); String inputLine; StringBuffer content = new StringBuffer(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); } in.close();` – Eric Stoppel Jul 03 '19 at 18:53