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:
OpenweathermapProviderType
YahooWeatherProviderType
WeatherundergroundProviderType
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:
com.survivingwithandroid.weather.lib.client.volley.WeatherClientDefault
com.survivingwithandroid.weather.lib.client.okhttp.WeatherClientDefault
com.survivingwithandroid.weather.lib.StandardHttpClient
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
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)
}
dependencies {
compile 'com.survivingwithandroid:weatherlib:1.6.0'
compile 'com.survivingwithandroid:weatherlib_okhttpclient:1.6.0'
compile 'com.squareup.okhttp:okhttp:2.0.+'
}
dependencies {
compile 'com.survivingwithandroid:weatherlib:1.6.0'
}
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);
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" />
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