Weather library

Weatherlib
Get current weather




Introduction

In this paragraph we will see how to get current weather conditions. Weatherlib helps you to get weather condition
creating weather data model indipendently from the weather provider you used.
You can query the current weather condition using city id, retrieved using search city, or using latitude and longitude. The library will check the way you are querying the condition and choses the right remote service to invoke.
Notice that city id has the precedence respect to geo coords, so if you create a request, using WeatherRequest, holding both information, the library will select the city id to create the query.

The Current weather features is supported by:

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

Get current weather by city id

If you want to get current weather condition using city id, first you have to create the WeatherRequest:

WeatherRequest req = new WeatherRequest(cityId);

Now we have the request, we can query the current weather. As always, our weatherClient object must be correctly instantiated and configured. The method to use to get the weather is:

   weatherClient.getCurrentCondition(req, new WeatherClient.WeatherEventListener() {
@Override
public void onWeatherRetrieved(CurrentWeather cWeather) {
Weather weather = cWeather.weather;
// The weather condition object is ready and we can use it
}

@Override
public void onWeatherError(WeatherLibException t) {
// Something went wrong maybe we should inform the user
}

@Override
public void onConnectionError(Throwable t) {
// There was a connection error
}
});


As you can see it is very simple, we have to provide a listener so that we get informed when the weather data is ready.
The onWeatherRetrieved(CurrentWeather cWeather) is called as soon as the data is ready.
We can use CurrentWeather to get the current weather condition. This class holds WeatherUnit that is filled with the unit measure system retrieved from the weather provider. Weather class hold all the information about current weather. Weather is built as:
public class Weather {
    public Location location = new Location();
    public Condition currentCondition = new Condition();
    public Temperature temperature = new Temperature();
    public Wind wind = new Wind();
    public Rain[] rain = {new Rain(), new Rain()};
    public Snow snow = new Snow();
    public Clouds clouds = new Clouds();
..
}

As you can see there are all weather information in the Android app
For example, in the onWeatherRetrieved in your Android weather app, you should fill your Android UI app:
weatherClient.getCurrentCondition(req, new WeatherClient.WeatherEventListener() {
            @Override
            public void onWeatherRetrieved(CurrentWeather cWeather) {
                Weather weather = cWeather.weather;
                getListener().requestCompleted();
                cityText.setText(weather.location.getCity() + "," + weather.location.getCountry());
                condDescr.setText(weather.currentCondition.getCondition() + "(" + weather.currentCondition.getDescr() + ")");
                LogUtils.LOGD("SwA", "Temp [" + temp + "]");
                LogUtils.LOGD("SwA", "Val [" + weather.temperature.getTemp() + "]");
                temp.setText("" + ((int) weather.temperature.getTemp()));
                unitTemp.setText(cWeather.getUnit().tempUnit);
                colorTextLine.setBackgroundResource(WeatherUtil.getResource(weather.temperature.getTemp(), config));
                hum.setText(weather.currentCondition.getHumidity() + "%");
                tempMin.setText(weather.temperature.getMinTemp() + cWeather.getUnit().tempUnit);
                tempMax.setText(weather.temperature.getMaxTemp() + cWeather.getUnit().tempUnit);
                windSpeed.setText(weather.wind.getSpeed() + cWeather.getUnit().speedUnit);
                windDeg.setText((int) weather.wind.getDeg() + "° (" + WindDirection.getDir((int) weather.wind.getDeg()) + ")");
                press.setText(weather.currentCondition.getPressure() + cWeather.getUnit().pressureUnit);

                sunrise.setText(WeatherUtil.convertDate(weather.location.getSunrise()));

                sunset.setText(WeatherUtil.convertDate(weather.location.getSunset()));
                ....
             }
}

You should notice, that some fields could be null depending on the weather provider.