【问题标题】:Volley Object request Returning 0Volley 对象请求返回 0
【发布时间】:2015-11-15 15:58:25
【问题描述】:

我有一个包含 6 行的远程数据库。
这是我的代码,我把它放在 List 中以便我可以得到大小,
但它总是返回 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;
    }

我需要这个尺寸以备不时之需。
为什么总是返回零?
我什至尝试在 for 循环中添加一个 counter++。
但是当我得到计数​​器的值时,它仍然为零。

【问题讨论】:

    标签: android arrays json android-studio android-volley


    【解决方案1】:

    JsonObjectRequest 在后台线程上运行。这就是为什么您将列表大小设为 0。您必须使用 public void onResponse(JSONObject response) {} 函数。

    示例

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

    【讨论】:

    • 据我所知 Volley 是最好的,而且谷歌也在推广使用这个库。链接:developer.android.com/training/volley/index.html 另外,无论您使用什么从远程数据库中提取数据,都必须运行后台线程(我的意思是异步线程)。
    • 如果您在 onResponse 方法中的 for 循环之后仔细检查,您将获得数据库中的数据列表。因此,您的行数将是int row = commentsData.size();,您可以在获取数据后做任何您想做的事情。
    • 是的,问题是,没有 UI 线程就无法启动 ui 组件。您需要通过调用 runOnUIThread(Runnable); 来执行此操作。 runOnUiThread(new Runnable() { @Override public void run() { recyclerView.setAdapter(adapter); // set the adapter here. } });
    • 我实际上无法得到你的答案。静态值是什么意思?你的意思是这个-> String staticValue = textView.getText(); 但我不知道为什么你的涟漪效应不起作用。与凌空无关。
    • 欢迎您。如果你愿意,你可以给 +1 投票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2022-01-01
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 2020-02-20
    相关资源
    最近更新 更多