- try {
- // Construct the URL for the OpenWeatherMap query
- // Possible parameters are avaiable at OWM's forecast API page, at
- // http://openweathermap.org/API#forecast
- final String FORECAST_BASE_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?";
- // final String QUERY_PARAM = "id"; // use this if using a location ID
- final String QUERY_PARAM = "q";
- final String FORMAT_PARAM = "mode";
- final String UNITS_PARAM = "units";
- final String DAYS_PARAM = "cnt";
-
- Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
- .appendQueryParameter(QUERY_PARAM, params[0])
- .appendQueryParameter(FORMAT_PARAM, format)
- .appendQueryParameter(UNITS_PARAM, units)
- .appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))
- .build();
-
- URL url = new URL(builtUri.toString());
-/**/ Log.v(LOG_TAG, "Built URI " + builtUri.toString());
-
- // 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, "Forecast JSON string: " + forecastJsonStr);
- try {
- return getWeatherDataFromJson(forecastJsonStr, numDays);
- } catch (JSONException e) {
- Log.e(LOG_TAG, e.getMessage(), e);
- e.printStackTrace();
- }