Volley Object request Returning 0

爱⌒轻易说出口 提交于 2019-12-24 15:28:02

问题


I Have a Remote Database that contains 6 rows.
Here's my code, I put it in List so that i can get the size,
But it's always returning 0.

public List<Comments> getComments() {
        final List<Comments> commentsData = new ArrayList<>();
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                showUrl, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArray = response.getJSONArray("poi");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        Comments current = new Comments();
                        current.image = "drawable://" + R.drawable.juandirection_placeholder;
                        current.title = jsonObject.getString("title");
                        current.comment = jsonObject.getString("comment");
                        current.date = jsonObject.getString("date");
                        current.rating = Integer.parseInt(jsonObject.getString("rating"));
                        commentsData.add(current);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        VolleyHelper.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
        return commentsData;
    }

I need the size for some use.
Why is it always returning zero?
I even tried adding a counter++ inside for loop.
But when i get the value of the counter its still zero.


回答1:


JsonObjectRequest run on a background thread. That's why you getting the list size 0. you must work into the public void onResponse(JSONObject response) {} function .

Exmp.

@Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray = response.getJSONArray("poi");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Comments current = new Comments();
                    current.image = "drawable://" + R.drawable.juandirection_placeholder;
                    current.title = jsonObject.getString("title");
                    current.comment = jsonObject.getString("comment");
                    current.date = jsonObject.getString("date");
                    current.rating = Integer.parseInt(jsonObject.getString("rating"));
                    commentsData.add(current);
                }

                // **you may call function and pass the list value to update your ui component. you will get the real size of list here.**

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


来源:https://stackoverflow.com/questions/33721683/volley-object-request-returning-0

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