Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void onClickShowWeather(View view) {
- String city = editTextCity.getText().toString().trim();
- Log.i("hoz_", "onClickShowWeather() ==> city = " + city);
- if (!city.isEmpty()) {
- DownloadWeatherTask task = new DownloadWeatherTask();
- String url = String.format(WEATHER_URL, city);
- Log.i("hoz_", "onClickShowWeather() ==> url = " + url);
- task.execute(url);
- }
- Log.i("hoz_", "onClickShowWeather() ==> The END");
- }
- private class DownloadWeatherTask extends AsyncTask<String, Void, String> {
- @Override
- protected String doInBackground(String... strings) {
- URL url = null;
- HttpURLConnection urlConnection = null;
- StringBuilder result = new StringBuilder();
- try {
- url = new URL(strings[0]);
- urlConnection = (HttpURLConnection) url.openConnection();
- InputStream inputStream = urlConnection.getInputStream();
- InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
- BufferedReader reader = new BufferedReader(inputStreamReader);
- String line = reader.readLine();
- while (line != null) {
- result.append(line);
- line = reader.readLine();
- }
- Log.i("hoz_", "doInBackground() ==> result = " + result.toString());
- return result.toString();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (urlConnection != null) {
- urlConnection.disconnect();
- }
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement