Advertisement
Guest User

Untitled

a guest
Mar 16th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. public void onClickShowWeather(View view) {
  2. String city = editTextCity.getText().toString().trim();
  3. Log.i("hoz_", "onClickShowWeather() ==> city = " + city);
  4. if (!city.isEmpty()) {
  5. DownloadWeatherTask task = new DownloadWeatherTask();
  6. String url = String.format(WEATHER_URL, city);
  7. Log.i("hoz_", "onClickShowWeather() ==> url = " + url);
  8. task.execute(url);
  9. }
  10. Log.i("hoz_", "onClickShowWeather() ==> The END");
  11. }
  12.  
  13. private class DownloadWeatherTask extends AsyncTask<String, Void, String> {
  14. @Override
  15. protected String doInBackground(String... strings) {
  16. URL url = null;
  17. HttpURLConnection urlConnection = null;
  18. StringBuilder result = new StringBuilder();
  19. try {
  20. url = new URL(strings[0]);
  21. urlConnection = (HttpURLConnection) url.openConnection();
  22. InputStream inputStream = urlConnection.getInputStream();
  23. InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
  24. BufferedReader reader = new BufferedReader(inputStreamReader);
  25. String line = reader.readLine();
  26.  
  27. while (line != null) {
  28. result.append(line);
  29. line = reader.readLine();
  30. }
  31. Log.i("hoz_", "doInBackground() ==> result = " + result.toString());
  32.  
  33. return result.toString();
  34. } catch (MalformedURLException e) {
  35. e.printStackTrace();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. } finally {
  39. if (urlConnection != null) {
  40. urlConnection.disconnect();
  41. }
  42. }
  43. return null;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement