使用AsyncTask从URL中获取数据到一个数组列表中。

问题描述 投票:0回答:1

下面列出的原始代码可以工作,它将在两行中分别显示7,加州,昨天和我从网站上获取的标题,当我试图将标题添加到任何一个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个回答
0
投票

你的代码工作正常......可能是其他地方出现了错误,也许是你没有提到的RecyclerAdapter......检查@Overridepublic int getItemCount() { return 0; / size of your arraylist}。

也许你使用的值超过了数组列表所包含的值。

© www.soinside.com 2019 - 2024. All rights reserved.