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 trialSharina V Jones
Courses Plus Student 1,847 PointsParsing 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!