【问题标题】:return modified arrayList outside of Volley onRsponse在 Volley onRsponse 之外返回修改后的 arrayList
【发布时间】:2015-09-23 13:18:34
【问题描述】:

我需要在 volley onResponse 方法之外访问这个 arrayList。但它返回 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 

        }

如您所见,我正在尝试登录两个地方。来自 onResponse 的日志确实返回了一些东西,但在 onResponse 之外它只是给出了 IndexOutOfBoundException。我做了很多调试,并意识到 onResponse arrayList 之外是空的(我试图访问一个不存在的空元素)。在 onResponse 内部修改的 ArrayList 在外部无法访问。甚至它是一个全局/类级别的变量

【问题讨论】:

    标签: android arraylist android-volley


    【解决方案1】:

    我建议您在 volley 成功响应后使用单独的方法,如下所示

    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); 
             }
    

    【讨论】:

    • 我很愚蠢。谢谢你的建议。它确实有所帮助,并且按我的预期工作。我想将您的建议标记为 ans,但我的代表太低,无法投票给 ans :( 再次感谢
    猜你喜欢
    • 2016-06-04
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    相关资源
    最近更新 更多