package com.example.weather;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private EditText cityname;
private Button button1;
private TextView textView2;
private TextView textView3;
private TextView textView4;
private TextView textView5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cityname = findViewById(R.id.cityname);
button1 = findViewById(R.id.button1);
textView2 = findViewById(R.id.textView2);
textView3 = findViewById(R.id.textView3);
textView4 = findViewById(R.id.textView4);
textView5 = findViewById(R.id.textView5);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (cityname.getText().toString().trim().equals(""))
Toast.makeText(MainActivity.this,R.string.warning, Toast.LENGTH_LONG).show();
else {
String city = cityname.getText().toString();
String key = "cba05fac32e986f325878b497331cfc8";
String url = "https://api.openweathermap.org/data/2.5/weather?q=" + city +"&appid="+ key +"&units=metric&lang=ru";
new GerUrlData().execute(url);
}
}
});
}
private class GerUrlData extends AsyncTask<String, String, String>{
protected void onPreExecute(){
super.onPreExecute();
textView2.setText("Ожидайте...");
textView3.setText("Ожидайте...");
textView4.setText("Ожидайте...");
textView5.setText("Ожидайте...");
}
@Override
protected String doInBackground(String... strings) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(strings[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null)
buffer.append(line).append("\n");
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(connection != null)
connection.disconnect();
try {
if(reader != null)
reader.close();
}catch (IOException e){
e.printStackTrace();
}
}
return null;
}
@SuppressLint("SetTextI18n")
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
try {
JSONObject jsonObject = new JSONObject(result);
textView2.setText("Температура: " + jsonObject.getJSONObject("main").getDouble("temp"));
textView3.setText("Ощущаеться: " + jsonObject.getJSONObject("main").getDouble("feels_like"));
textView4.setText("Влажность: " + jsonObject.getJSONObject("main").getDouble("humidity")+"%");
textView5.setText("Порыв ветра: " + jsonObject.getJSONObject("wind").getDouble("gust")+"м/c");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}