Last active
June 17, 2016 20:53
-
-
Save DLMousey/10ff5ac33383d99bdb605fc1264e0d78 to your computer and use it in GitHub Desktop.
Missing RetrieveForecastTask.java class for the Complete Android M Dev Bundle on StackSkills, With additional comments
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
package com.tmlov.androidweather; | |
import android.os.AsyncTask; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.ArrayList; | |
/** | |
* Created by DLMousey on 17/06/2016. | |
*/ | |
public class RetrieveForecastTask extends AsyncTask<Double, Void, ArrayList<Weather>> { | |
// doInBackground is an android function that allows us access to the Background Thread so tasks | |
// can be executed asynchronously and won't lock up the main thread which would lock up the display | |
// and piss our users off. | |
@Override | |
protected ArrayList<Weather> doInBackground(Double... params) { | |
// Define a variable called list that's of type ArrayList<Weather> | |
ArrayList<Weather> list = new ArrayList<Weather>(); | |
// Return our list | |
try { | |
URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?lat=" + params[0] + "&lon=" + params[1] + "&units=imperial&cnt=7"); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.setReadTimeout(10000); | |
connection.setConnectTimeout(15000); | |
connection.setRequestMethod("GET"); | |
connection.setDoInput(true); | |
connection.connect(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return list; | |
} | |
// In the event it all goes tits up, this object should throw a JSONException | |
private ArrayList<Weather> parseJSON(String json) throws JSONException { | |
// Define a variable called forecast that's of type ArrayList<Weather> | |
ArrayList<Weather> forecast = new ArrayList<Weather>(); | |
JSONArray jsonArray = new JSONObject(json).getJSONArray("list"); | |
// If our object contains less items than the array of data we get back, | |
// start grabbing values from the returned data | |
for(int i = 0; i < jsonArray.length(); i++) { | |
// Define a new variable called Weather that's of type Weather | |
Weather weather = new Weather(); | |
// Make a new JSONObject called jsonDay which is our returned data | |
JSONObject jsonDay = jsonArray.getJSONObject(i); | |
weather.setTimestamp(jsonDay.getInt("dt")); | |
weather.setHigh(jsonDay.getJSONObject("temp").getDouble("max")); | |
weather.setLow(jsonDay.getJSONObject("temp").getDouble("min")); | |
JSONObject jsonWeather = jsonDay.getJSONArray("weather").getJSONObject(0); | |
weather.setWeather(jsonWeather.getString("main")); | |
forecast.add(weather); | |
} | |
return forecast; | |
} | |
// Define a variable called convertStreamToString that's of type String | |
// In the event this all goes tits up, This object should throw an IOException | |
private String convertStreamToString(InputStream is) throws IOException { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(is)); | |
StringBuilder builder = new StringBuilder(); | |
String line; | |
while((line = reader.readLine()) != null) { | |
builder.append(line); | |
} | |
return builder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment