Getting started

To create a WeatherClient we need a few lines. The WeatherClient must be used to get weather information.

The first thing, we have to instantiate the builder. The builder is used to create the client:

WeatherClient.ClientBuilder builder = new WeatherClient.ClientBuilder();

then we have to instantiate the WeatherConfig. If you instantiate a WeatherConfig without setting parameters, the default paramters will be used. The WeatherConfig is a class that is used to control the general behaviour of the client. Using this class, you can set if the metric or imperial system must be used or the number of days in the forecast. By now we can simply instantiate the class without setting parameters so that the default values will be used.

WeatherConfig config = new WeatherConfig();

Now we can create our client and the weather information. As explained before, other than the configuration we have to specify the transport library we want to used and the weather provider. For example:

WeatherClient client = builder.attach(getActivity())
.provider(new ForecastIOProviderType())
.httpClient(com.survivingwithandroid.weather.lib.client.okhttp.WeatherDefaultClient.class)
.config(config)
.build();

In this new way we use a builder to create the Weatherclient. At line 2, we set the Weather provider: as you already know there are four different weather provider supported by now by the lib:

  • Openweathermap -> OpenweathermapProviderType
  • Yahoo! Weather -> YahooWeatherProviderType
  • Weatherundergroung -> WeatherundergroundProviderType
  • Forecast.io -> ForecastIOProviderType

At line 3 we set the HTTP client that the library uses to connect to the remote server: there a three different clients supported by the lib:

  • Volley Client -> com.survivingwithandroid.weather.lib.client.volley.WeatherClientDefault
  • (available from 1.0 version)
  • OkHttp Client -> com.survivingwithandroid.weather.lib.client.okhttp.WeatherClientDefault
  • (available from 1.5 version)
  • Standard client -> com.survivingwithandroid.weather.lib.StandardHttpClient
  • (available from 1.0 version)

One important aspect we must notice is that the first two client are asynchronous client, while the last one is synchronous.

In the build.gradle we have to add dependecies, according to the type of the client you choosed

Volley HTTP Library

dependencies {					
compile 'com.survivingwithandroid:weatherlib:1.6.0'
compile 'com.survivingwithandroid:weatherlib_volleyclient:1.6.0'
compile 'com.mcxiaoke.volley:library:1.0.6@aar' (or whatever you like)
}

OkHttp HTTP Library

dependencies {					
compile 'com.survivingwithandroid:weatherlib:1.6.0'
compile 'com.survivingwithandroid:weatherlib_okhttpclient:1.6.0'
compile 'com.squareup.okhttp:okhttp:2.0.+'
}

Standard client

dependencies {					
compile 'com.survivingwithandroid:weatherlib:1.6.0'
}

Configuration

Going deeper into the configuration, we can configure our client using WeatherConfig attribute, as shown below:

WeatherConfig config = new WeatherConfig();
config.unitSystem = WeatherConfig.UNIT_SYSTEM.M;
config.lang = "en"; // If you want to use english
config.maxResult = 5; // Max number of cities retrieved
config.numDays = 6; // Max num of days in the forecast

We can change the configuration paramter without rebuilding the client, but we have to update the configuration in our client and provider using updateWeatherConfig and passing the new configuration:

client.updateWeatherConfig(config);	

Permissions in Manifest

One aspect we have to consider is the permission in Manifest.xml so that the lib can connect to internet and use GPS device if you need:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Simple Weatherlib activity sample

If you want to go and use the lib directly here there is an example that shows how to create an Activity that gets current weather information