0

from a wordpress site, using json rest api, we get the json of the whole site. I want to load the json of first 10 posts from all category for android. I am using volley to load the json array and it is failing to load the whole array response as it is huge is size. I want first 10 posts and when I click load more I want the json of 11th post to 20th post. can I do so?

currently my url is like http://www.example.com/wp-json/posts

I am requesting in following code

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

    });

Currently I got an exception like com.android.volley.TimeoutError

And What the parameter requestBody does in JsonArrayRequest ? why we use null here?

explaining with codes will be better for me.

Thanks in advance.

Mihodi Lushan
  • 670
  • 5
  • 24

1 Answers1

1

Why do we use null there? Because your request is using the HTTP-Method "Get" which passes Arguments via URL. If you use HTTP-Method "Post" you would put your request data into that JSONObject so volley can append that to HTTP-Body-Data.

you could try setting a custom RetryPolicy, since Volley timeouts after 4 seconds of waiting for repsponse - see here

Pinning down the exact problem is kinda hard with the provided information. I cant see a problem with your Request itself.

Shot in the blue: Argument 3 (String) null, maybe pass in an empty json object.

I would definitely try to investigate deeper into volley by setting it to verbose via adb:

adb -s shell setprop log.tag.Volley VERBOSE

that way volley provides further debug information in your LogCat.

Good Luck

Community
  • 1
  • 1
Hannes
  • 311
  • 3
  • 9
  • thanks a lot @hannes, now the posts are loading in the list. Sometimes it throws TimeOutError and then still it loads. It will be very pleasure for me if you say why we send a null object to request JSON Object in 3rd parameter. And in the detailed post except from the infographic image post other images are loading. Can you please help me optimizing images to load faster. I used picasso library. – Mihodi Lushan Jul 11 '16 at 16:44
  • Can You Please Help Me [here](http://stackoverflow.com/questions/38312625/loading-large-images-in-textview-using-html-imagegetter-and-picasso-library-in-a) – Mihodi Lushan Jul 11 '16 at 17:18
  • Sure, but i dont know Picasso for imageloading, For me the solution would be volley Image loader. It can load multiple images in batches and so on. There are very nice documentations on that. [Picasso vs Volley](https://www.bignerdranch.com/blog/solving-the-android-image-loading-problem-volley-vs-picasso/) I would need further information... – Hannes Jul 11 '16 at 17:24
  • Actually I need a imageloader as Html.ImageGetter. Can I do it with volley image loader? I dont have image view. I have to convert everything in textview – Mihodi Lushan Jul 11 '16 at 18:27