Weather library

Weatherlib
Forecast




Introduction

In this paragraph we will see how to get forecast. We can get weather forecast in android in two different ways:
  • Next days forecast
  • Hourly forecast

Weatherlib supports both type of request. As told before, you have to use WeatherRequest to get weather forecast.
The Daily forecast features is supported by:

Openweathermap Yes
Weatherunderground
Weather.com
Yes
Yahoo! Weather Yes
Forecast.io Yes


The Hourly forecast features is supported by:

Openweathermap Yes
Weatherunderground
Weather.com
Yes
Yahoo! Weather Yes
Forecast.io Yes


Daily weather forecast

Let us suppose we retrieved already the city id retrieved using search city.
The code is very simple:
       weatherClient.getForecastWeather(new WeatherRequest(cityId), new WeatherClient.ForecastWeatherEventListener() {
            @Override
            public void onWeatherRetrieved(WeatherForecast forecast) {
                // Data is ready
            }

            @Override
            public void onWeatherError(WeatherLibException t) {

            }

            @Override
            public void onConnectionError(Throwable t) {
                //WeatherDialog.createErrorDialog("Error parsing data. Please try again", MainActivity.this);
            }
        });
    }

Once the data is ready, we can handle it. WeatherForecast holds the weather forecast represented as an java List.
So we have:
List dayForecastList = forecast.getForecast();

Now we can traverse the List:
for (DayForecast dForecast : dayForecastList) {
    Weather weather = dForecast.weather;
    long timestamp = dForecast.timestamp;
}

where the Weather class represents the weather in they day specified by the timestamp. In the onWeatherRetrieved we can instantiate an Android custom adapter and fill a Android Listview.

Hourly forecast

The hourly forecast is used in the same way we used the day forecast.
       weatherClient.getHourForecastWeather(new WeatherRequest(cityId), new WeatherClient.HourForecastWeatherEventListener() {
            @Override
            public void onWeatherRetrieved(WeatherHourForecast weatherHourForecast) {
                List hourList = weatherHourForecast.getHourForecast();
                for (HourForecast hourForecast: hourList) {
                    Weather weather = hourForecast.weather;
                    long timestamp = hourForecast.timestamp;
                }
            }

            @Override
            public void onWeatherError(WeatherLibException e) {

            }

            @Override
            public void onConnectionError(Throwable throwable) {

            }
        });