Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private class GetWeatherTask extends AsyncTask<String, Void, String> {
- private TextView textView;
- public GetWeatherTask(TextView textView) {
- this.textView = textView;
- }
- @Override
- protected String doInBackground(String... strings) {
- String weather = "UNDEFINED";
- try {
- URL url = new URL(strings[0]);
- HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
- InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
- StringBuilder builder = new StringBuilder();
- String inputString;
- while ((inputString = bufferedReader.readLine()) != null) {
- builder.append(inputString);
- }
- JSONObject topLevel = new JSONObject(builder.toString());
- JSONObject main = topLevel.getJSONObject("estimated");
- weather = String.valueOf(topLevel.getString("temperature_string"));
- urlConnection.disconnect();
- } catch (IOException | JSONException e) {
- e.printStackTrace();
- }
- return weather;
- }
Advertisement
Add Comment
Please, Sign In to add comment