private void appendForecastTemperature(SimpleWeatherCondition condition, JSONObject json) throws JSONException { WeatherParser parser = new ForecastWeatherParser(json); AppendableTemperature existedTemp = (AppendableTemperature) condition.getTemperature(); SimpleTemperature newTemp = parser.parseTemperature(); existedTemp.append(newTemp); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.weather); InputStream weather = checkWeather(); WeatherParser weatherParser = new WeatherParser(weather); TextView conditionsText = (TextView) findViewById(R.id.conditionsText); conditionsText.setText(weatherParser.getConditions()); TextView temperatureText = (TextView) findViewById(R.id.temperatureText); temperatureText.setText(weatherParser.getTemperature() + "ºC"); TextView humidityText = (TextView) findViewById(R.id.humidityText); humidityText.setText(weatherParser.getHumidity()); }
@Test public void should_get_weather_info() throws IOException { WeatherParser parser = new WeatherParser(); Weather weather; try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("new_orleans_weather.xml")) { weather = parser.parse(inputStream); } assertEquals("New Orleans", weather.getCity()); assertEquals("LA", weather.getRegion()); assertEquals("US", weather.getCountry()); assertEquals(66, weather.getTemperature(), 0.001); assertEquals("Mostly Cloudy", weather.getCondition()); assertEquals("81", weather.getHumidity()); assertEquals("9", weather.getWindSpeed()); }
void parseCurrentWeather(JSONObject json) throws WeatherException { try { int code = json.getInt("cod"); if (code != 200) { this.empty = true; return; } parseCityId(json); parseLocation(json); parseTime(json); WeatherParser parser = new CurrentWeatherParser(json); parser.parseCondition(); } catch (JSONException e) { throw new WeatherException("cannot parse the weather", e); } this.empty = false; }
private void parseForecast(SimpleWeatherCondition condition, JSONObject json) throws JSONException { appendForecastTemperature(condition, json); WeatherParser parser = new ForecastWeatherParser(json); condition.setHumidity(parser.parseHumidity()); condition.setWind(parser.parseWind()); condition.setPrecipitation(parser.parsePrecipitation()); condition.setCloudiness(parser.parseCloudiness()); parser.parseWeatherType(condition); }
@Override protected String[] doInBackground(String... strings) { // 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; String format = "json"; String units = "metric"; String numDays = "7"; 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 = "q"; final String FORMAT_PARAM = "format"; final String UNITS_PARAM = "units"; final String DAYS_PARAM = "cnt"; Uri.Builder builder = Uri.parse(FORECAST_BASE_URL) .buildUpon() .appendQueryParameter(QUERY_PARAM, strings[0]) .appendQueryParameter(FORMAT_PARAM, format) .appendQueryParameter(UNITS_PARAM, units) .appendQueryParameter(DAYS_PARAM, numDays); URL url = new URL(builder.build().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. forecastJsonStr = 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. forecastJsonStr = null; } forecastJsonStr = buffer.toString(); } catch (IOException e) { Log.e(LOG_TAG, "Error ", e); // If the code didn't successfully get the weather data, there's no point in attemping // to parse it. forecastJsonStr = null; } finally { if (urlConnection != null) { urlConnection.disconnect(); } if (reader != null) { try { reader.close(); } catch (final IOException e) { Log.e(LOG_TAG, "Error closing stream", e); } } } String[] weather = null; if (forecastJsonStr != null) { try { weather = WeatherParser.getWeatherDataFromJson(forecastJsonStr, 7); } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); e.printStackTrace(); } } return weather; }