Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Weather App Working with JSON Setting CurrentWeather from JSON

Sharina V Jones
Sharina V Jones
1,797 Points

Parsing AccuWeather Data

Just wanted to post the way I parsed data from the AccuWeather Current Conditions API.

Here's the URL: "http://dataservice.accuweather.com/currentconditions/v1/{locationKey}?apikey={API}"

I got my location key by using the main AccuWeather site to search for my weather info. Here's the URL I used with my info redacted. https://www.accuweather.com/en/us/{yourCity}/{yourZipCode}/weather-forecast/{locationKeyAppearsHereAfterSearch}

Here's the current format of the JSON data

[
  {
    "LocalObservationDateTime": "2022-03-23T07:39:00-04:00",
    "EpochTime": 1648035540,
    "WeatherText": "Mostly cloudy",
    "WeatherIcon": 6,
    "HasPrecipitation": false,
    "PrecipitationType": null,
    "IsDayTime": true,
    "Temperature": {
      "Metric": {
        "Value": 11.1,
        "Unit": "C",
        "UnitType": 17
      },
      "Imperial": {
        "Value": 52,
        "Unit": "F",
        "UnitType": 18
      }
    },
  }
]

Here is the URL for my API call.

String forcastURL = "http://dataservice.accuweather.com/currentconditions/v1/"
            + locationKey + "?apikey=" + apiKey

Here's my CurrentWeather function

private CurrentWeather getCurrentDetails(String jsonData) throws JSONException {
        JSONArray forecast = new JSONArray(jsonData);
        JSONObject forcastData = forecast.getJSONObject(0);

        String localTime = forcastData.getString("LocalObservationDateTime");
        String weatherText = forcastData.getString("WeatherText");
        Log.i(TAG, "From JSON: " + localTime);

        CurrentWeather currentWeather = new CurrentWeather();
        currentWeather.setLocalTime(localTime);
        currentWeather.setEpochTime(forcastData.getInt("EpochTime"));
        currentWeather.setHasPrecipitation(forcastData.getBoolean("HasPrecipitation"));
        currentWeather.setPrecipType(forcastData.getString("PrecipitationType"));
        currentWeather.setDayTime(forcastData.getBoolean("IsDayTime"));
        currentWeather.setTempValue(forcastData.getJSONObject("Temperature")
            .getJSONObject("Imperial")
            .getDouble("Value"));
        currentWeather.setTempUnit(forcastData.getJSONObject("Temperature")
            .getJSONObject("Imperial")
            .getString("Unit"));
        currentWeather.setLocationLabel("City, State");

        return currentWeather;
    }

Hope this helps!