Openweathermap | Yes |
Weatherunderground Weather.com |
No |
Yahoo! Weather | NO |
Forecast.io | NO |
WeatherRequest wRequest = new WeatherRequest("/q/zmw:00000.1.16181"); WebcamFeatureRequest req = new WebcamFeatureRequest(wRequest, config); WebcamResponseParser parser = new WebcamResponseParser();
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) { } });
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; } }
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; } }