Weather library

Weatherlib
Extended feature




Introduction

This paragraph introduces a new aspect of Android Weatherlib. By now we talked about features shipped with the library, now it is time to describe some specific weather provider features. These are features that are tighly bound to the weather provider: i.e. Webcam images etc. Weatherlib provides some example how to use this feature with Weatherunderground or Weather.com.
You are free to extend and implement your specific provider features. Think about it like a plugin extension where you can implement your logic.

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


Bear in mind that where it is not supported, just means the code isn't still developed and it is up to you to implement the logic!

Webcam images

To get a Webcam images from a city you simply have to:

WeatherRequest wRequest = new WeatherRequest("/q/zmw:00000.1.16181");
WebcamFeatureRequest req = new WebcamFeatureRequest(wRequest, config);
WebcamResponseParser parser = new WebcamResponseParser();

where config is an instanceof WeatherConfig. In the case of Weatherunderground, it is necessary to pass a key that represents your account, and you pass it through config instance.
It is necessary to create a custom feature request i.e. WebcamFeatureRequest and a custom response parser WebcamResponseParser, that is able to parse the response from the weather provider.
Once we have the request and the parser, it is time to make the request:

client.getProviderWeatherFeature(wRequest, req, parser, new WeatherClient.GenericRequestWeatherEventListener>() {
           @Override
           public void onResponseRetrieved(List data) {
               Log.d("Webcam", "Data ["+data+"]");
               for (WebcamFeatureResponse resp : data) {
                   Log.d("Wecbam", "Name ["+resp.city+"] - URL ["+resp.currentImageUrl+"]");

               }
               if (data != null && data.size() > 0) {
                   // We take the first element
                   WebcamFeatureResponse resp = data.get(0);
                   client.getImage(resp.currentImageUrl, new WeatherClient.WeatherImageListener() {
                       @Override
                       public void onImageReady(Bitmap image) {
                           webcamView.setImageBitmap(image);
                       }

                       @Override
                       public void onWeatherError(WeatherLibException wle) {
                            // Never used in this case
                       }

                       @Override
                       public void onConnectionError(Throwable t) {
                           Toast.makeText(getActivity(), "Connection error", Toast.LENGTH_SHORT).show();
                       }
                   });
               }
           }

           @Override
           public void onWeatherError(WeatherLibException wle) {
               Toast.makeText(getActivity(), "Error parsing the response", Toast.LENGTH_SHORT).show();
               wle.printStackTrace();
           }

           @Override
           public void onConnectionError(Throwable t) {

           }
       });
 


where WebcamFeatureResponse holds the data extracted by the parser from the response.

Custom weather request

To create a custom weather request to a specific provider, other than those already supported by Android Weatherlib you have to create a class that extends WeahterundergroundProviderFeature that in turn extends WeatherProviderFeature. Below, there is an example of WebcamFeatureRequest

public class WebcamFeatureRequest extends WeatherUndergroundProviderFeature {

    public WebcamFeatureRequest(WeatherRequest request, WeatherConfig config) {
        super(request, config);
    }

    @Override
    public String getURL() throws ApiKeyRequiredException {
        if (config.ApiKey == null || config.ApiKey.equals(""))
            throw new ApiKeyRequiredException();

        String url = "http://api.wunderground.com/api" + "/" + config.ApiKey + "/webcams/";
        url = addLanguage(url);

        if (request.getCityId() != null)
            url = url + request.getCityId() + ".json";
        else
            url = url + request.getLat() + "," + request.getLon() + ".json";

        return url;
    }
}

Custom weather response parser

Once the custom weather request is ready, it is time to create the custom response parser, i.e WebcamResponseParser.

public class WebcamResponseParser extends GenericResponseParser> {
    @Override
    public List parseData(String data) throws WeatherLibException {
        List result = new ArrayList();
        try {
            JSONObject jObj = new JSONObject(data);
            JSONArray webcams = jObj.getJSONArray("webcams");
            for (int i=0; i < webcams.length(); i++) {
                JSONObject webcam = webcams.getJSONObject(i);

                WebcamFeatureResponse resp = new WebcamFeatureResponse();
                resp.city = webcam.getString("city");
                resp.state = webcam.getString("state");
                resp.country = webcam.getString("country");
                resp.camId = webcam.getString("camid");
                resp.handle = webcam.getString("handle");
                resp.link = webcam.getString("link");
                resp.linkText = webcam.getString("linktext");
                resp.currentImageUrl = webcam.getString("CURRENTIMAGEURL");
                resp.widgetCurrentImageUrl = webcam.getString("WIDGETCURRENTIMAGEURL");

                result.add(resp);
            }
        }
        catch(Throwable t) {
            throw new WeatherLibException(t);
        }

        return result;
    }
}


This is a useful extension point of Weatherlib.