oimtrust

YahooWeatherService_WeatherYahooApp

Nov 27th, 2016
1,580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. package com.oimtrust.weatheryahoo.service;
  2.  
  3. import android.net.Uri;
  4. import android.os.AsyncTask;
  5.  
  6. import com.oimtrust.weatheryahoo.data.Channel;
  7.  
  8. import org.json.JSONException;
  9. import org.json.JSONObject;
  10.  
  11. import java.io.BufferedReader;
  12. import java.io.InputStream;
  13. import java.io.InputStreamReader;
  14. import java.net.URL;
  15. import java.net.URLConnection;
  16.  
  17. /**
  18.  * Created by Oim on 10/12/2016.
  19.  */
  20.  
  21. public class YahooWeatherService {
  22.     private WeatherServiceCallback callback;
  23.     private String location;
  24.     private Exception error;
  25.  
  26.  
  27.     public YahooWeatherService(WeatherServiceCallback callback) {
  28.         this.callback = callback;
  29.     }
  30.  
  31.     public String getLocation() {
  32.         return location;
  33.     }
  34.  
  35.     public void refreshWeather(String l){
  36.         this.location = l;
  37.         new AsyncTask<String, Void, String>() {
  38.             @Override
  39.             protected String doInBackground(String... strings) {
  40.  
  41.                 String YQL  = String.format("select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\"%s\") and u='c'", strings[0]);
  42.  
  43.                 String endpoint = String.format("https://query.yahooapis.com/v1/public/yql?q=%s&format=json", Uri.encode(YQL));
  44.  
  45.                 try {
  46.                     URL url = new URL(endpoint);
  47.                     URLConnection connection    = url.openConnection();
  48.  
  49.                     InputStream inputStream = connection.getInputStream();
  50.                     BufferedReader reader   = new BufferedReader(new InputStreamReader(inputStream));
  51.                     StringBuilder result    = new StringBuilder();
  52.                     String line;
  53.                     while ((line = reader.readLine()) != null){
  54.                         result.append(line);
  55.                     }
  56.  
  57.                     return result.toString();
  58.  
  59.                 } catch (Exception e) {
  60.                     error = e;
  61.                 }
  62.  
  63.                 return null;
  64.             }
  65.  
  66.             @Override
  67.             protected void onPostExecute(String s) {
  68.  
  69.                 if (s == null && error != null){
  70.                     callback.serviceFailure(error);
  71.                     return;
  72.                 }
  73.  
  74.                 try {
  75.                     JSONObject data = new JSONObject(s);
  76.  
  77.                     JSONObject queryResult  = data.optJSONObject("query");
  78.  
  79.                     int count = queryResult.optInt("count");
  80.                     if (count == 0){
  81.                         callback.serviceFailure(new LocationWeatherException("No weather information found for " + location));
  82.                         return;
  83.                     }
  84.  
  85.                     Channel channel = new Channel();
  86.                     channel.populate(queryResult.optJSONObject("results").optJSONObject("channel"));
  87.                     callback.serviceSuccess(channel);
  88.                 } catch (JSONException e) {
  89.                     callback.serviceFailure(e);
  90.                 }
  91.             }
  92.         }.execute(location);
  93.     }
  94.  
  95.     public class LocationWeatherException extends Exception{
  96.         public LocationWeatherException(String message) {
  97.             super(message);
  98.         }
  99.     }
  100. }
Add Comment
Please, Sign In to add comment