java - 为什么即使我没有运行错误,应用程序也不显示任何内容?
问题描述
我正在尝试做一个天气应用程序,在我获取数据并尝试显示它之后,我也尝试在 Log 中查看它,因为它什么都不显示,但在这里它也什么也不显示。我也没有运行错误。这是代码:
package com.example.weatherapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONObject;
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 {
TextView cityName;
Button searchButton;
TextView result;
class Weather extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... adress) {
try {
URL url = new URL(adress[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int data = isr.read();
String content = "";
char ch;
while (data != -1){
ch = (char) data;
content = content + ch;
data = isr.read();
}
return content;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
public void search (View view){
cityName = findViewById(R.id.cityName);
searchButton = findViewById(R.id.searchButton);
result = findViewById(R.id.result);
String cName = cityName.getText().toString();
String content;
Weather weather = new Weather();
try {
content = weather.execute("https://openweathermap.org/data/2.5/weather?q=" + cName + "&appid=439d4b804bc8187953eb36d2a8c26a02").get();
JSONObject jsonObject = new JSONObject(content);
String weatherData = jsonObject.getString("weather");
JSONArray array = new JSONArray(weatherData);
String main ="";
String description ="";
for (int i = 0; i<=array.length(); i++){
JSONObject weatherPart = array.getJSONObject(i);
main = weatherPart.getString("main");
description = weatherPart.getString("description");
}
Log.w("main", main);
Log.w("description", description);
result.setText("Main : "+ main + "\nDescription : "+ description);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
searchButton onClickListener 是在 XML 中实现的,所以它可以工作,这是我运行应用程序时日志显示的内容:
2020-04-20 21:46:22.482 12518-12611/com.example.weatherapp D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-04-20 21:46:22.896 12518-12518/com.example.weatherapp W/System.err: org.json.JSONException: Index 1 out of range [0..1)
2020-04-20 21:46:22.896 12518-12518/com.example.weatherapp W/System.err: at org.json.JSONArray.get(JSONArray.java:293)
2020-04-20 21:46:22.896 12518-12518/com.example.weatherapp W/System.err: at org.json.JSONArray.getJSONObject(JSONArray.java:521)
2020-04-20 21:46:22.896 12518-12518/com.example.weatherapp W/System.err: at com.example.weatherapp.MainActivity.search(MainActivity.java:88)
2020-04-20 21:46:22.896 12518-12518/com.example.weatherapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2020-04-20 21:46:22.896 12518-12518/com.example.weatherapp W/System.err: at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
2020-04-20 21:46:22.896 12518-12518/com.example.weatherapp W/System.err: at android.view.View.performClick(View.java:5610)
2020-04-20 21:46:22.896 12518-12518/com.example.weatherapp W/System.err: at android.view.View$PerformClick.run(View.java:22265)
2020-04-20 21:46:22.896 12518-12518/com.example.weatherapp W/System.err: at android.os.Handler.handleCallback(Handler.java:751)
2020-04-20 21:46:22.896 12518-12518/com.example.weatherapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
2020-04-20 21:46:22.896 12518-12518/com.example.weatherapp W/System.err: at android.os.Looper.loop(Looper.java:154)
2020-04-20 21:46:22.896 12518-12518/com.example.weatherapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6077)
2020-04-20 21:46:22.896 12518-12518/com.example.weatherapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2020-04-20 21:46:22.896 12518-12518/com.example.weatherapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
2020-04-20 21:46:22.896 12518-12518/com.example.weatherapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
我找不到任何线索为什么这不起作用。你能帮助我吗?
解决方案
您的 for 循环在 终止i <= array.length();
,这会导致JSONException
, 因为循环越过数组的长度。
由于 Java 数组中的第一项位于零索引上,因此它们的最后一项位于 的索引处array.length() - 1
,因此在您的情况下,您应该结束 for 循环i < array.length();
推荐阅读
- c++ - 在c ++中使用getline函数进行输入时忽略第一个字符
- node.js - Azure Devops - 部署前清理目标目录
- javascript - 有没有办法将 firstName 和 lastName 的第一个字母大写并使其成为 Javascript 中的首字母
- flutter - 为什么我不能在“只有静态成员可以在初始化程序中访问”的颤动中加载扩展面板中的实际数据?
- python - selenium 打开浏览器控制台并使用 python 粘贴一些 javascript 代码
- javascript - 防止 Webpack ProvidePlugin 替换 node_modules 上的标识符
- python - Flask 开发服务器重新加载时向前端发送消息
- java - Excel ::String单元格值未与Java中的整数连接
- r - 如何为 R 中的函数编写参数验证。可以使用胶水吗?
- c# - 不能为 IDictionary 使用扩展方法