Текстовое представление не отображает значение, которое мне нужно. Я пытаюсь получить прогноз погоды, показанный внутри него. Однако, когда я пытаюсь это сделать, приложение вообще ничего не отображает.
<TextView
android:id="@+id/degree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading..."
android:textSize="60sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Вниз мой основной вид деятельности - класс java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = findViewById(R.id.degree);
WeatherTask task = new WeatherTask(tv);
task.execute("moscow");
}
}
Вот основной класс Java, который получает прогноз для выбранного города
public class WeatherTask extends AsyncTask<String, Void, String> {
private static final String API_KEY = "06c7d6512a914b3029937dd444283ff0";
private final TextView textView;
public WeatherTask(TextView tv){
this.textView = tv;
}
@Override
protected String doInBackground(String... cities) {
String weather = "";
try {
String urlString = String.format(
"http://api.openweathermap.org/data/2.5/weather?q=%s,uz&units=%s&appid=%s",
cities[0], "metric", API_KEY
);
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
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);
}
String response = builder.toString();
JSONObject topLevel = new JSONObject(response);
JSONObject main = topLevel.getJSONObject("main");
weather = String.valueOf(main.getDouble("temp"));
urlConnection.disconnect();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return weather;
}
@Override
protected void onPostExecute(String weather)
{
this.textView.setText(weather);
}
}
Может кто-нибудь объяснить, почему текстовое представление не отображается в основной деятельности?
Спасибо