Guest User

Untitled

a guest
Sep 1st, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. private class GetWeatherTask extends AsyncTask<String, Void, String> {
  2.         private TextView textView;
  3.  
  4.         public GetWeatherTask(TextView textView) {
  5.             this.textView = textView;
  6.         }
  7.  
  8.         @Override
  9.         protected String doInBackground(String... strings) {
  10.             String weather = "UNDEFINED";
  11.             try {
  12.                 URL url = new URL(strings[0]);
  13.                 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  14.  
  15.                 InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
  16.                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
  17.                 StringBuilder builder = new StringBuilder();
  18.  
  19.                 String inputString;
  20.                 while ((inputString = bufferedReader.readLine()) != null) {
  21.                     builder.append(inputString);
  22.                 }
  23.  
  24.                 JSONObject topLevel = new JSONObject(builder.toString());
  25.                 JSONObject main = topLevel.getJSONObject("estimated");
  26.                 weather = String.valueOf(topLevel.getString("temperature_string"));
  27.  
  28.                 urlConnection.disconnect();
  29.             } catch (IOException | JSONException e) {
  30.                 e.printStackTrace();
  31.             }
  32.  
  33.             return weather;
  34.         }
Advertisement
Add Comment
Please, Sign In to add comment