即使movie_suggestion已满,也要获取IndexOutOfBound Exeption

时间:2019-05-18 14:41:42

标签: java android indexoutofboundsexception

我正在尝试从json文件中获取数据。

一切顺利,直到我将最终的字符串值放入namek,然后将其添加到名为movie_suggestion的arraylist中为止。当我为movie_suggestion的任何值做Toast时,它只会显示IndexOutOfBoundsException。

Tag

这是错误:

RequestQueue requestQueue2;

Cache cache = new DiskBasedCache(getContext().getCacheDir(), 1024 * 1024);

Network network = new BasicNetwork(new HurlStack());

requestQueue2 = new RequestQueue(cache, network);

requestQueue2.start();

final ProgressDialog progressDialog2 = new ProgressDialog(getContext());
    progressDialog2.setMessage("Searching...");
    progressDialog2.setCancelable(false);
    progressDialog2.show();

StringRequest stringRequest2 = new StringRequest(Request.Method.GET, url_100,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    Toast.makeText(getContext(), "Nope!",Toast.LENGTH_SHORT).show();
                    progressDialog2.dismiss();
                    try {
                        JSONObject jsonObject = new JSONObject(s);
                        JSONArray array = jsonObject.getJSONArray("movies");

                        for (int i = 0; i < array.length(); i++) {
                            JSONObject o = array.getJSONObject(i);
                            String namek = o.getString("name");
                            movie_suggestion.add(namek);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // Handle error
                }
            });


Toast.makeText(getContext(), movie_suggestion.get(0), 
Toast.LENGTH_SHORT).show();
requestQueue2.add(stringRequest2);

0 个答案:

没有答案