|
15 | 15 | */ |
16 | 16 | package com.example.android.sunshine.app; |
17 | 17 |
|
| 18 | +import android.os.AsyncTask; |
18 | 19 | import android.os.Bundle; |
19 | 20 | import android.support.v4.app.Fragment; |
20 | 21 | import android.util.Log; |
@@ -76,65 +77,74 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, |
76 | 77 | ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast); |
77 | 78 | listView.setAdapter(mForecastAdapter); |
78 | 79 |
|
79 | | - // These two need to be declared outside the try/catch |
80 | | - // so that they can be closed in the finally block. |
81 | | - HttpURLConnection urlConnection = null; |
82 | | - BufferedReader reader = null; |
83 | | - |
84 | | - // Will contain the raw JSON response as a string. |
85 | | - String forecastJsonStr = null; |
86 | | - |
87 | | - try { |
88 | | - // Construct the URL for the OpenWeatherMap query |
89 | | - // Possible parameters are avaiable at OWM's forecast API page, at |
90 | | - // http://openweathermap.org/API#forecast |
91 | | - URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7"); |
92 | | - |
93 | | - // Create the request to OpenWeatherMap, and open the connection |
94 | | - urlConnection = (HttpURLConnection) url.openConnection(); |
95 | | - urlConnection.setRequestMethod("GET"); |
96 | | - urlConnection.connect(); |
97 | | - |
98 | | - // Read the input stream into a String |
99 | | - InputStream inputStream = urlConnection.getInputStream(); |
100 | | - StringBuffer buffer = new StringBuffer(); |
101 | | - if (inputStream == null) { |
102 | | - // Nothing to do. |
103 | | - return null; |
104 | | - } |
105 | | - reader = new BufferedReader(new InputStreamReader(inputStream)); |
106 | | - |
107 | | - String line; |
108 | | - while ((line = reader.readLine()) != null) { |
109 | | - // Since it's JSON, adding a newline isn't necessary (it won't affect parsing) |
110 | | - // But it does make debugging a *lot* easier if you print out the completed |
111 | | - // buffer for debugging. |
112 | | - buffer.append(line + "\n"); |
113 | | - } |
| 80 | + return rootView; |
| 81 | + } |
| 82 | + |
| 83 | + public class FetchWeatherTask extends AsyncTask<Void, Void, Void> { |
| 84 | + |
| 85 | + private final String LOG_TAG = FetchWeatherTask.class.getSimpleName(); |
| 86 | + |
| 87 | + @Override |
| 88 | + protected Void doInBackground(Void... params) { |
| 89 | + // These two need to be declared outside the try/catch |
| 90 | + // so that they can be closed in the finally block. |
| 91 | + HttpURLConnection urlConnection = null; |
| 92 | + BufferedReader reader = null; |
| 93 | + |
| 94 | + // Will contain the raw JSON response as a string. |
| 95 | + String forecastJsonStr = null; |
| 96 | + |
| 97 | + try { |
| 98 | + // Construct the URL for the OpenWeatherMap query |
| 99 | + // Possible parameters are avaiable at OWM's forecast API page, at |
| 100 | + // http://openweathermap.org/API#forecast |
| 101 | + URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7"); |
| 102 | + |
| 103 | + // Create the request to OpenWeatherMap, and open the connection |
| 104 | + urlConnection = (HttpURLConnection) url.openConnection(); |
| 105 | + urlConnection.setRequestMethod("GET"); |
| 106 | + urlConnection.connect(); |
| 107 | + |
| 108 | + // Read the input stream into a String |
| 109 | + InputStream inputStream = urlConnection.getInputStream(); |
| 110 | + StringBuffer buffer = new StringBuffer(); |
| 111 | + if (inputStream == null) { |
| 112 | + // Nothing to do. |
| 113 | + return null; |
| 114 | + } |
| 115 | + reader = new BufferedReader(new InputStreamReader(inputStream)); |
| 116 | + |
| 117 | + String line; |
| 118 | + while ((line = reader.readLine()) != null) { |
| 119 | + // Since it's JSON, adding a newline isn't necessary (it won't affect parsing) |
| 120 | + // But it does make debugging a *lot* easier if you print out the completed |
| 121 | + // buffer for debugging. |
| 122 | + buffer.append(line + "\n"); |
| 123 | + } |
114 | 124 |
|
115 | | - if (buffer.length() == 0) { |
116 | | - // Stream was empty. No point in parsing. |
| 125 | + if (buffer.length() == 0) { |
| 126 | + // Stream was empty. No point in parsing. |
| 127 | + return null; |
| 128 | + } |
| 129 | + forecastJsonStr = buffer.toString(); |
| 130 | + } catch (IOException e) { |
| 131 | + Log.e(LOG_TAG, "Error ", e); |
| 132 | + // If the code didn't successfully get the weather data, there's no point in attemping |
| 133 | + // to parse it. |
117 | 134 | return null; |
118 | | - } |
119 | | - forecastJsonStr = buffer.toString(); |
120 | | - } catch (IOException e) { |
121 | | - Log.e("ForecastFragment", "Error ", e); |
122 | | - // If the code didn't successfully get the weather data, there's no point in attemping |
123 | | - // to parse it. |
124 | | - return null; |
125 | | - } finally{ |
126 | | - if (urlConnection != null) { |
127 | | - urlConnection.disconnect(); |
128 | | - } |
129 | | - if (reader != null) { |
130 | | - try { |
131 | | - reader.close(); |
132 | | - } catch (final IOException e) { |
133 | | - Log.e("ForecastFragment", "Error closing stream", e); |
| 135 | + } finally { |
| 136 | + if (urlConnection != null) { |
| 137 | + urlConnection.disconnect(); |
| 138 | + } |
| 139 | + if (reader != null) { |
| 140 | + try { |
| 141 | + reader.close(); |
| 142 | + } catch (final IOException e) { |
| 143 | + Log.e(LOG_TAG, "Error closing stream", e); |
| 144 | + } |
134 | 145 | } |
135 | 146 | } |
| 147 | + return null; |
136 | 148 | } |
137 | | - |
138 | | - return rootView; |
139 | 149 | } |
140 | 150 | } |
0 commit comments