return modified arrayList outside of Volley onRsponse

最后都变了- 提交于 2019-12-12 03:17:42

问题


I need to access this arrayList outside of volley onResponse method. But it's returning IndexOutOfBoundsException.

public ArrayList<QuestionAnswer> arrayList;

    protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                arrayList = new ArrayList<>();

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, getTalkById, new JSONObject(params),
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                JSONObject data = response.optJSONObject("data");
                                JSONArray highSchoolQuesAns = data.optJSONArray("questions").optJSONObject(0).optJSONArray("answers");

                                for (int i = 0; i < highSchoolQuesAns.length(); i++) {

                                    String question = highSchoolQuesAns.optJSONObject(i).optString("question");
                                    String answer = highSchoolQuesAns.optJSONObject(i).optString("answer");

                                    arrayList.add(new QuestionAnswer(question, answer)); 
    // adding new QuestionAnswer 
                                }
Log.v("test",arrayList.get(0).question); // value is available here inside on response. And getting question of first arrayList element.
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                VolleyLog.d("Error", "Error: " + error.getMessage());
                            }
                        }
                );
                request.setRetryPolicy(new DefaultRetryPolicy(
                        VolleyApplication.TIMEOUT,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
                VolleyApplication.getInstance().getRequestQueue().add(request);


                Log.v("test",arrayList.get(0).question); //java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
//This is where the issue is. AND I REALLY NEED TO access this arraylist outside of volley onResponse method 

        }

as you can see I am trying to log in two places. Log from onResponse does return something but outside of onResponse it just gives IndexOutOfBoundException. I did debug a lot and realized that outside of onResponse arrayList is empty(and I am trying to access an empty element which doesn't exist). ArrayList that was modified inside onResponse is not accessible outside. Even it's a global/class level variable


回答1:


I suggest you to have separate method after success response from volley as given below

public ArrayList<QuestionAnswer> arrayList;

    protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                arrayList = new ArrayList<>();

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, getTalkById, new JSONObject(params),
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                JSONObject data = response.optJSONObject("data");
                                JSONArray highSchoolQuesAns = data.optJSONArray("questions").optJSONObject(0).optJSONArray("answers");

                                for (int i = 0; i < highSchoolQuesAns.length(); i++) {

                                    String question = highSchoolQuesAns.optJSONObject(i).optString("question");
                                    String answer = highSchoolQuesAns.optJSONObject(i).optString("answer");

                                    arrayList.add(new QuestionAnswer(question, answer)); 
    // adding new QuestionAnswer 
                                }
                                GetArrayValue();
Log.v("test",arrayList.get(0).question); // value is available here inside on response. And getting question of first arrayList element.
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                VolleyLog.d("Error", "Error: " + error.getMessage());
                            }
                        }
                );
                request.setRetryPolicy(new DefaultRetryPolicy(
                        VolleyApplication.TIMEOUT,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
                VolleyApplication.getInstance().getRequestQueue().add(request);

        }

        private void GetArrayValue(){
         Log.v("test",arrayList.get(0).question); 
         }


来源:https://stackoverflow.com/questions/32731424/return-modified-arraylist-outside-of-volley-onrsponse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!