Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Weather App (2015) Working with JSON Cleaning Up the Date and Time

yotam laor
yotam laor
1,319 Points

JSON Exception caught

hi,

I try to run the following code and get a JSON exception: No value for America/Los_Angeles (I am currently not in Los Angeles).

private CurrentWeather getCurrentDetails(String jsonData) throws JSONException {

    JSONObject forecast = new JSONObject(jsonData);

    String timeZone =forecast.getString("timezone");

    Log.i(TAG,"From JSON: " + timeZone);

    JSONObject currently = forecast.getJSONObject("currently");

    CurrentWeather currentWeather = new CurrentWeather();
    currentWeather.setHumidity(currently.getDouble("humidity"));
    currentWeather.setTime(currently.getLong("time"));
    currentWeather.setIcon(currently.getString("icon"));
    currentWeather.setPrecipitation(currently.getDouble("precipProbability"));
    currentWeather.setSummary(currently.getString("summary"));
    currentWeather.setTemperature(currently.getDouble("temperature"));
    currentWeather.setTtimeZone(currently.getString(timeZone));

    Log.d(TAG, currentWeather.getFormattedTime());

    return new CurrentWeather();
}

the exception is thrown from the line: "currentWeather.setTtimeZone(currently.getString(timeZone));".

Does anyone have a solution for this problem?

Thanks

1 Answer

Hi there,

I assume your method in CurrentWeather is setTtimeZone? That would throw a different error; suggest correcting the typo as that may cause confusion.

You can either use the timezone variable you have already created:

String timeZone =forecast.getString("timezone");

in the currentWeather line:

currentWeather.setTtimeZone(currently.getString(timeZone)); // change this
currentWeather.setTtimeZone(timeZone); // to this

Or you can make sure you're pulling the time zone from currently with this:

currentWeather.setTtimeZone(currently.getString("timezone"));

I can't answer your LA question; that depends on the lat/long you sent the API.

I hope that helps.

Steve.