Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Retrieving Json data from URL - Java

Options

Comments

  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    You can use a library like jsoup to get the resultant content back as a string.
    Here's a Stack overflow example for JSON https://stackoverflow.com/questions/7133118/jsoup-requesting-json-response


  • Registered Users Posts: 6,062 ✭✭✭Talisman


    Your application should use the JSON API client library to retrieve the data from the OpenWeatherMap service.

    Once you have retrieved the data from the API request you should cache it. If you have a web server like Apache/Nginx etc. in front of Jersey it will cache the response for you provided you set the correct Cache-Control headers. The benefit of this is two fold, you reduce the impact of requests on your application and also reduce the volume of requests to the OpenWeatherMap service - such free services are usually request rate limited.

    Cache-Control Using Annotations With Jersey

    To return a valid content type you need to set the appropriate HTTP header: "Content-Type: application/json"

    Code outline without caching:
    @Path("/WeatherAPI")
    public class Weather {
        @GET
        @Path("/{City}")
        @Produces(MediaType.APPLICATION_JSON)
        public Response getWeather(@PathParam("City") String City) {
    
            // Call the API
            String OpenWeatherResponse = ...
    
            // Return the response string
            return Response.status(Status.OK).entity(OpenWeatherResponse).build();
    
        }
    }
    


Advertisement