我正在尝试制作一个天气应用程序,在获取数据并尝试显示它之后,我也尝试在日志中查看它,因为它什么都不显示,但是在这里它也什么也不显示。我在跑步中也没有错误。 这是代码:
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)
我找不到任何线索为什么这行不通。你能帮我吗?
答案 0 :(得分:2)
您的for循环终止于i <= array.length();
,当循环遍历数组的长度时,其结果为JSONException
。
由于Java数组中的第一项位于零索引上,因此它们的最后一项位于array.length() - 1
的索引上,因此,在您的情况下,应在i < array.length();
处结束for循环