-
-
Save anonymous/9d251540d992efd5ffab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* The date/time conversion code is going to be moved outside the asynctask later, | |
* so for convenience we're breaking it out into its own method now. | |
*/ | |
private String getReadableDateString(long time){ | |
// Because the API returns a unix timestamp (measured in seconds), | |
// it must be converted to milliseconds in order to be converted to valid date. | |
Date date = new Date(time * 1000); | |
SimpleDateFormat format = new SimpleDateFormat("E, MMM d"); | |
return format.format(date).toString(); | |
} | |
/** | |
* Prepare the weather high/lows for presentation. | |
*/ | |
private String formatHighLows(double high, double low) { | |
// For presentation, assume the user doesn't care about tenths of a degree. | |
long roundedHigh = Math.round(high); | |
long roundedLow = Math.round(low); | |
String highLowStr = roundedHigh + "/" + roundedLow; | |
return highLowStr; | |
} | |
/** | |
* Take the String representing the complete forecast in JSON Format and | |
* pull out the data we need to construct the Strings needed for the wireframes. | |
* | |
* Fortunately parsing is easy: constructor takes the JSON string and converts it | |
* into an Object hierarchy for us. | |
*/ | |
private String[] getWeatherDataFromJson(String forecastJsonStr, int numDays) | |
throws JSONException { | |
// These are the names of the JSON objects that need to be extracted. | |
final String OWM_LIST = "list"; | |
final String OWM_WEATHER = "weather"; | |
final String OWM_TEMPERATURE = "temp"; | |
final String OWM_MAX = "max"; | |
final String OWM_MIN = "min"; | |
final String OWM_DATETIME = "dt"; | |
final String OWM_DESCRIPTION = "main"; | |
JSONObject forecastJson = new JSONObject(forecastJsonStr); | |
JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST); | |
String[] resultStrs = new String[numDays]; | |
for(int i = 0; i < weatherArray.length(); i++) { | |
// For now, using the format "Day, description, hi/low" | |
String day; | |
String description; | |
String highAndLow; | |
// Get the JSON object representing the day | |
JSONObject dayForecast = weatherArray.getJSONObject(i); | |
// The date/time is returned as a long. We need to convert that | |
// into something human-readable, since most people won't read "1400356800" as | |
// "this saturday". | |
long dateTime = dayForecast.getLong(OWM_DATETIME); | |
day = getReadableDateString(dateTime); | |
// description is in a child array called "weather", which is 1 element long. | |
JSONObject weatherObject = dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0); | |
description = weatherObject.getString(OWM_DESCRIPTION); | |
// Temperatures are in a child object called "temp". Try not to name variables | |
// "temp" when working with temperature. It confuses everybody. | |
JSONObject temperatureObject = dayForecast.getJSONObject(OWM_TEMPERATURE); | |
double high = temperatureObject.getDouble(OWM_MAX); | |
double low = temperatureObject.getDouble(OWM_MIN); | |
highAndLow = formatHighLows(high, low); | |
resultStrs[i] = day + " - " + description + " - " + highAndLow; | |
} | |
return resultStrs; | |
} |
@korovyansk, these are facts that cannot be denied.
Very true, highlighted gists do look better and read more easily!
I agree! Looks like @AlexKorovyansky's gist has been moved, so here's a new one: https://gist.github.com/nicksuch/bd4da8d493aa1bb708b0
Use Gson library instead of parsing it manually.
private Collection<String> decodeJson(String json) {
final Gson gson = new Gson();
final Forecast weather = gson.fromJson(json, Forecast.class);
final ArrayList<String> decodedJson = new ArrayList<String>();
for (DailyForecast df : weather.list) {
decodedJson.add(String.format("%s %s %s", df.temp.day, df.temp.min, df.temp.max));
}
return decodedJson;
}
private class Forecast {
public String cod;
public String message;
public List<DailyForecast> list;
}
private class DailyForecast {
public Double dt;
public Temperature temp;
public Double pressure;
public Double humidity;
public Double speed;
public Double deg;
public Double clouds;
}
private class Temperature {
public Double day;
public Double min;
public Double max;
public Double night;
public Double eve;
public Double morn;
}
private class Weather {
public Double id;
public String main;
public String description;
public String icon;
}
JSONObject weatherObject = dayForecast.getJSONArray(OWM_WEATHER).getJSONObject(0);
description = weatherObject.getString(OWM_DESCRIPTION);
Why u put 0 in getJSONObject()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
why .txt, not .java? highlighted gists look much better — https://gist.github.com/korovyansk/e0b32963195729742a70