java - 为什么即使我没有运行错误,应用程序也不显示任何内容?

标签 java json android-studio

我正在尝试做一个天气应用程序,在获取数据并尝试显示它之后,我还尝试在日志中查看它,因为它什么也不显示,但在这里它也什么也不显示。我运行也没有错误。 这是代码:

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 处。 ,所以在你的情况下,你应该在 i < array.length(); 处结束 for 循环

关于java - 为什么即使我没有运行错误,应用程序也不显示任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61329907/

相关文章:

javascript - 如何在不知道对象名称的情况下返回 Json 对象子对象?

Android Studio : Logcat search vs. 过滤器

java - android studio mergedebugresources java.lang.ArrayIndexOutOfBoundsException(无错误信息)

java - 静态 block 和实例变量的执行顺序

Java - 包含同一父类(super class)的一组不同子类的数据结构

javascript - 使用lodash过滤json数组中具有多个键的多个值

json - 我可以在 golang 的 json 中将用户定义的值作为数据类型吗

java - 使用 Java 对 C++ 代码进行单元测试

java - 序列化和通过 JDBC 将对象保存到 JAVA_OBJECT 之间的区别

android - Gradle 同步失败 : Using insecure protocols with repositories, 没有明确选择加入,不受支持。切换 Maven 存储库