0
{"location":{

"name":"New York","region":"New York","country":"United States of America","lat":40.71,"lon":-74.01,"tz_id":"America/New_York","localtime_epoch":1488124171,"localtime":"2017-02-26 10:49"},

"forecast":{

"forecastday":[{"date":"2017-02-26","date_epoch":1488067200,"day":{"maxtemp_c":5.2,"mintemp_c":1.0,"avgtemp_c":3.5,"maxwind_kph":28.1,

"astro":{"sunrise":"06:34 AM",...

I am programming a Weather App, but when I want to access the, for Example, date then Android Studio says that there is : No value for forecast. This is how I try to get the data:

JSONObject jsonObject = new JSONObject(data);

[...]

JSONObject forecastObject = Utils.getObject("forecast", jsonObject);

JSONArray forecastArray = forecastObject.getJSONArray("forecastday");

JSONObject forecastWeather = forecastArray.getJSONObject(0);

weather.currentCondition.setDate(Utils.getString("date", forecastWeather));

JSONObject dayObject = Utils.getObject("day", forecastWeather);

What am I doing wrong?? Can You help me?

1

2 Answers 2

0

Are you sure, that you want item at index 0 of this JSONArray? To get JSONArray from JSONObject, you call JSONArray array = object.getJSONArray("key"), then JSONObject newobject = array.getJSONObject(index).

Sign up to request clarification or add additional context in comments.

3 Comments

I want the first item from "forecastday" isnt it 0 then?
@ZockerBros Can you upload full json, validated via json validator?
I got did it the same way he did : link
0
public static Weather getWeather(String data){
    Weather weather = new Weather();
    //creat JSONObject from data
    try {
        JSONObject jsonObject = new JSONObject(data);

        Place place = new Place();


        JSONObject locationObject = Utils.getObject("location", jsonObject);
        place.setLat(Utils.getFloat("lat", locationObject));
        place.setLon(Utils.getFloat("lon", locationObject));
        place.setCity(Utils.getString("name", locationObject));
        place.setCountry(Utils.getString("country", locationObject));
        place.setRegion(Utils.getString("region", locationObject));
        place.setLocaltime(Utils.getString("localtime", locationObject));
        weather.place=place;


        JSONObject currentObject = Utils.getObject("current", jsonObject);
        weather.currentCondition.setLastupdated(Utils.getString("last_updated", currentObject));
        weather.currentCondition.setTemperature(Utils.getString("temp_c", currentObject));
        weather.currentCondition.setWindkph(Utils.getString("wind_kph", currentObject));
        weather.currentCondition.setHumidity(Utils.getFloat("humidity", currentObject));
        weather.currentCondition.setCloud(Utils.getInt("cloud", currentObject));
        weather.currentCondition.setFeelslike(Utils.getDouble("feelslike_c", currentObject));
        weather.wind.setWinddir(Utils.getString("wind_dir", currentObject));
        weather.currentCondition.setPreciption(Utils.getString("precip_mm", currentObject));
        weather.currentCondition.setPressure(Utils.getFloat("pressure_mb", currentObject));
        JSONObject iconObject = Utils.getObject("condition", currentObject);
        weather.currentCondition.setIcon(Utils.getString("icon", iconObject));
        weather.currentCondition.setDescription(Utils.getString("text", iconObject));


        return weather;

    } catch (JSONException e) {
        e.printStackTrace();

        return null;
    }
}

This is the function of how i get my data from the website apixu.com

This is how i connect:

public class WeatherHttpClient {

public String getWeatherData(String place){
    HttpURLConnection connection = null;
    InputStream inputStream = null;

    try {
        connection = (HttpURLConnection) (new URL(Utils.BASE_URL + place)).openConnection();
        connection.setRequestMethod("GET");
        connection.setDoInput(true);
        connection.setDoInput(true);
        connection.connect();

        //Read response
        StringBuffer stringBuffer = new StringBuffer();
        inputStream = connection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line = null;
        while((line = bufferedReader.readLine())!=null){
            stringBuffer.append(line+ "\r\n");
        }

        inputStream.close();
        connection.disconnect();
        return stringBuffer.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.