【问题标题】:JSON of 10 posts from wp json rest api v1 For Android App来自 wp json rest api v1 For Android App 的 10 个帖子的 JSON
【发布时间】:2016-11-13 15:30:43
【问题描述】:

从一个wordpress站点,使用json rest api,我们得到整个站点的json。我想为android加载所有类别的前10个帖子的json。我正在使用 volley 加载 json 数组,但它无法加载整个数组响应,因为它的大小很大。我想要前 10 个帖子,当我单击加载更多时,我想要第 11 个帖子到第 20 个帖子的 json。我可以这样做吗?

目前我的网址是http://www.example.com/wp-json/posts

我在下面的代码中请求

JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET,
            baseUrl,
            (String) null,
            new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response) {
                    Log.d("json",response.toString());
                    listRecentPost = parseJsonResponse(response);

                    Log.d("LSN", listRecentPost.isEmpty() + "");
                    // If any data is avialable
                    if (!listRecentPost.isEmpty()) {


                        postAdapter.setRecentPost(listRecentPost);
                        postAdapter.notifyDataSetChanged();

                        /*
                          suppose data connection is off so error image and text will show
                        * but when my connection will be okk then i need to disable this image and error text
                        * */
                        errorImage.setVisibility(View.GONE);
                        errorMsg.setVisibility(View.GONE);
                    }

                    else {

                        errorMsg.setVisibility(View.VISIBLE);
                        errorMsg.setText("No Post Available");
                    }

                    //disable loading icon
                    swipeRefreshLayout.setRefreshing(false);
                }

            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("LSN", error.toString() + "VolleyError");
            // enable error iamge and text
            errorImage.setVisibility(View.VISIBLE);
            errorMsg.setVisibility(View.VISIBLE);

            if (error instanceof TimeoutError || error instanceof NoConnectionError) {

                errorMsg.setText(error.toString());

            } else if (error instanceof AuthFailureError) {

                errorMsg.setText(error.toString());


            } else if (error instanceof ServerError) {

                errorMsg.setText(error.toString());

            } else if (error instanceof NetworkError) {
                errorMsg.setText(error.toString());

            } else if (error instanceof ParseError) {

                errorMsg.setText(error.toString());
            }


            //again try to load data after 30 seconds
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    swipeRefreshLayout.setRefreshing(false);
                    swapeRefresh();
                }
            }, 30000);
        }

    });

目前我遇到了 com.android.volley.TimeoutError 之类的异常

JsonArrayRequest 中 requestBody 参数的作用是什么?为什么我们在这里使用 null?

用代码解释对我来说会更好。

提前致谢。

【问题讨论】:

    标签: java android json wordpress android-volley


    【解决方案1】:

    为什么我们在那里使用 null? 因为您的请求使用的是通过 URL 传递参数的 HTTP 方法“Get”。如果您使用 HTTP-Method "Post",您会将您的请求数据放入该 JSONObject 中,以便 volley 可以将其附加到 HTTP-Body-Data。

    您可以尝试设置自定义 RetryPolicy,因为 Volley 在等待响应 4 秒后超时 - see here

    使用所提供的信息很难确定确切的问题。我看不出您的请求本身有问题。

    在蓝色中射击: 参数 3(String) null,可能传入一个空的 json 对象。

    我肯定会尝试通过 adb 将其设置为详细来深入研究 volley:

    adb -s shell setprop log.tag.Volley VERBOSE
    

    这样 volley 会在您的 LogCat 中提供更多调试信息。

    祝你好运

    【讨论】:

    • 非常感谢@hannes,现在帖子正在加载到列表中。有时它会抛出 TimeOutError 然后仍然加载。如果您说出为什么我们在第三个参数中发送一个空对象来请求 JSON 对象,我将非常高兴。在详细的帖子中,除了信息图图像帖子之外,其他图像正在加载。你能帮我优化图像以加快加载速度吗?我用毕加索图书馆。
    • 你能帮我吗here
    • 当然,但我不知道 Picasso 用于图像加载,对我来说,解决方案是凌空图像加载器。它可以批量加载多个图像等。有非常好的文档。 Picasso vs Volley我需要更多信息...
    • 实际上我需要一个图像加载器作为 Html.ImageGetter。我可以用凌空图像加载器来做吗?我没有图像视图。我必须在 textview 中转换所有内容
    猜你喜欢
    • 2018-06-24
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    相关资源
    最近更新 更多