-3

I have develop the weather App and I want to use this URL, http://api.openweathermap.org/data/2.5/weather?lat=11.11111&lon=22.2222

and get the information only, country,sunrise,sunset,temp_min,temp_max,name. How get the response?

please suggest me. Thanks.

{
        sys: {
            message: 1.3106,
            country: "IN",
            sunrise: 1426123283,
            sunset: 1426166271
        },

        main: {
            temp: 306.561,
            temp_min: 306.561,
            temp_max: 306.561,
            pressure: 1016.81,
            sea_level: 1026.98,
            grnd_level: 1016.81,
            humidity: 43
        },

        name: "Sarkhej",
    }
Reena
  • 563
  • 4
  • 11
  • 22

2 Answers2

1
JSONObject one = new JSONObject(json);

JSONObject sys = one.getJSONObject("sys");
String message = sys.getString("message");
String country = sys.getString("country");
String sunrise = sys.getString("sunrise");
String sunset = sys.getString("sunset");

JSONObject main = one.getJSONObject("main");
String temp = main.getString("temp");
String temp_min = main.getString("temp_min");
String temp_max = main.getString("temp_max");
String pressure = main.getString("pressure");
...

String name=one.getString("name");
Manoj Bhadane
  • 607
  • 5
  • 11
0

You have to use HTTPClient to fetch data from url.

    HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost("http://api.openweathermap.org/data/2.5/weather?lat=11.11111&lon=22.2222");
                try {
                  HttpResponse response= client.execute(post);
                  String json = EntityUtils.toString(response.getEntity());
                    // you can parse this json string and can use it
String country = new JsonObject(json).getJsonObject("sys").getString("country");
                } catch (IOException e) {
                    e.printStackTrace();
                }

Don't forget to add internet permission in manifest.

Crazy Coder
  • 508
  • 1
  • 4
  • 12