【问题标题】:Fetching data from URL into an array-list with AsyncTask使用 AsyncTask 从 URL 获取数据到数组列表中
【发布时间】:2020-10-01 00:06:40
【问题描述】:

下面列出的原始代码有效,它将显示 7, california, 昨天以两行单独的行和我从网站获取的标题,当我尝试将标题添加到任何 ArrayLists 时出现问题,例如这个:

            try {
                JSONObject json = new JSONObject(s);

                String title = json.getJSONObject("metadata").getString("title");
                text.setText(title);

                magnitude.add(title);

            } catch (JSONException e) {
                e.printStackTrace();
            }

这会给我一个错误:java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 我不知道为什么。有什么解决办法吗?

原始代码:

public class MainActivity extends AppCompatActivity {


    ArrayList<String> magnitude;
    ArrayList<String> place;
    ArrayList<String> time;
    TextView text;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = findViewById(R.id.textView);
        magnitude = new ArrayList<>();
        place = new ArrayList<>();
        time = new ArrayList<>();

        getJson json = new getJson();
        String result = "";

        try {
            result = json.execute("https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&eventtype=earthquake&orderby=time&minmag=6&limit=10").get();
        } catch (InterruptedException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
        } catch (ExecutionException e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
        }
    }




    public void initRecycleView() {
        RecyclerView recycler = findViewById(R.id.list);
        recyclerAdapter adapter = new recyclerAdapter(MainActivity.this, magnitude, place, time);
        recycler.setAdapter(adapter);
        recycler.setLayoutManager(new LinearLayoutManager(MainActivity.this));

    }


    public class getJson extends AsyncTask<String, Void, String> {

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            magnitude.add("7");
            place.add("California");
            time.add("yesterday");

            try {
                JSONObject json = new JSONObject(s);

                String title = json.getJSONObject("metadata").getString("title");
                text.setText(title);


            } catch (JSONException e) {
                e.printStackTrace();
            }

            magnitude.add("7");
            place.add("California");
            time.add("yesterday");



            initRecycleView();
        }

        @Override
        protected String doInBackground(String... strings) {


            String result = "";
            URL url;
            HttpURLConnection connection = null;

            try {
                url = new URL(strings[0]);
                connection = (HttpURLConnection) url.openConnection();
                InputStream in = connection.getInputStream();
                InputStreamReader reader = new InputStreamReader(in);
                int data = reader.read();

                while(data != -1) {
                    char character = (char) data;
                    result += character;

                    data = reader.read();
                }
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return "";
            } catch (IOException e) {
                e.printStackTrace();
                return "";
            }
        }

    }
}

【问题讨论】:

    标签: android url arraylist android-asynctask


    【解决方案1】:

    您的代码工作正常.. 可能是来自其他地方的错误。可能来自你没有提到的 RecyclerAdapter.. 查看 @覆盖 公共 int getItemCount() { 返回0; // 数组列表的大小 }

    也许您使用的值比 arraylist 包含的更多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-26
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      相关资源
      最近更新 更多