-
Notifications
You must be signed in to change notification settings - Fork 0
/
FetchWeatherTask.java
89 lines (77 loc) · 3.7 KB
/
FetchWeatherTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
public class FetchWeatherTask extends AsyncTask<String,Void,Void>{
@Override
protected Void doInBackground(String... params) {
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String forecastJsonStr = null;
String countryCode=params[0];
int daysCount=7;
try {
final String COUNTRY_CODE="q";
final String MODE="mode";
final String UNIT="units";
final String DAYS_COUNT="cnt";
final String API_KEY="APPID";
// Construct the URL for the OpenWeatherMap query
// Possible parameters are avaiable at OWM's forecast API page, at
// http://openweathermap.org/API#forecast
Uri.Builder builder=new Uri.Builder();
builder.scheme("http")
.authority("api.openweathermap.org")
.path("data/2.5/forecast/daily")
.appendQueryParameter(COUNTRY_CODE, countryCode)
.appendQueryParameter(MODE, "json")
.appendQueryParameter(UNIT,"metric")
.appendQueryParameter(DAYS_COUNT,Integer.toString(daysCount))
.appendQueryParameter(API_KEY,"aed05dae3c7ca04436648f635661e3dc");
String stringUrl=builder.build().toString();
Log.v("built URL",stringUrl);
URL url = new URL(stringUrl);
// Create the request to OpenWeatherMap, and open the connection
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
forecastJsonStr = buffer.toString();
Log.v(LOG_TAG,"Weather Data: "+forecastJsonStr);
} catch (IOException e) {
Log.e("PlaceholderFragment", "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
} finally{
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("PlaceholderFragment", "Error closing stream", e);
}
}
}
return null;
}
}