Volley using StringRequest not calling getParams for sending POST Request Parameters after 1st time
Asked Answered
L

1

7

I am facing a problem where my POST request parameters are not going to server after 1st time. I know Volley is using cache mechanism for responses, but in my case my request parameter values can be changed at runtime as i am using pagination in Recyclerview.

So my questions is how can i send Post request parameter every time and wont loose cache mechanism of volley.

I have tried using below ones and get my things done (calling getParams() every-time).. but it loses caches response and i don't want that.

requestQueue.getCache().clear();

stringRequest.setShouldCache(false);

Also have Searched Google and below links but cant find any proper solution. below are the SO links

Below is my code:

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Log.e("RES", response);


                GsonBuilder gsonBuilder = new GsonBuilder();
                gsonBuilder.setDateFormat("M/d/yy hh:mm a"); //Format of our JSON dates
                Gson gson = gsonBuilder.create();

                NewsFeedPOJO resultObj = (NewsFeedPOJO) gson.fromJson(response, (Class) NewsFeedPOJO.class);

                inCurrPage = Integer.parseInt(resultObj.getPagination().getCurrent_page());
                inTotalPage = Integer.parseInt(resultObj.getPagination().getTotal_pages());
                inCurrPage++;

                arrayList.addAll(resultObj.getNewsFeedList());
                if (isFtym) {
                    isFtym = false;
                    layoutManager = new LinearLayoutManager(MainActivity.this);
                    rcNewsFeed.setLayoutManager(layoutManager);
                    adapter = new NewsFeedAdapter(MainActivity.this, arrayList);
                    rcNewsFeed.setAdapter(adapter);
                } else {
                    adapter.notifyItemInserted(arrayList.size());
                    adapter.notifyDataSetChanged();
                }

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("user_id", "188");

                if (inCurrPage == 0)
                    map.put("page", "1");
                else {
                    map.put("page", "" + inCurrPage);
                }

                Log.e("RES", inCurrPage + "  PARA");
                return map;
            }
        };
        //RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        //requestQueue.add(stringRequest);
        //requestQueue.getCache().clear();

        //AppController.getInstance().addToRequestQueue(stringRequest);
       // stringRequest.setShouldCache(false);
        VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);

using below Volley Dependency.

compile 'com.android.volley:volley:1.1.0'

If need more information please do let me know. Thanks in advance. Your efforts will be appreciated.

Launderette answered 6/1, 2018 at 11:18 Comment(5)
Not sure about Volley, but square.github.io/retrofit does handles these scenarios. Its more consolidated and efficient than Volley. Check out these comparison instructure.github.io/blog/2013/12/09/volley-vs-retrofitTeutonism
You should add a new request to the queue if you want to retrieve the next page. Not reuse the existing requestHansel
Have you tried this below link. https://mcmap.net/q/1628570/-prevent-volley-from-sending-cached-parameters-on-post-request-method-when-caching-is-implemented-in-volley/4140857Khaki
Have you used request.setRetryPolicy(new DefaultRetryPolicy(SERVER_TIMEOUT, MAX_RETRY, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));Liatrice
where MaxRetry is 0,1 or any you want for repeat calling, by default it is 1Liatrice
M
1

Did you check your Volley Singleton?

import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

public class VolleySingleton {
private static AppSingleton mAppSingletonInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mContext;

private AppSingleton(Context context) {
    mContext = context;
    mRequestQueue = getRequestQueue();

    mImageLoader = new ImageLoader(mRequestQueue,
            new ImageLoader.ImageCache() {
                private final LruCache<String, Bitmap>
                        cache = new LruCache<String, Bitmap>(20);

                @Override
                public Bitmap getBitmap(String url) {
                    return cache.get(url);
                }

                @Override
                public void putBitmap(String url, Bitmap bitmap) {
                    cache.put(url, bitmap);
                }
            });
}

public static synchronized AppSingleton getInstance(Context context) {
    if (mAppSingletonInstance == null) {
        mAppSingletonInstance = new AppSingleton(context);
    }
    return mAppSingletonInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        // getApplicationContext() is key, it keeps you from leaking the
        // Activity or BroadcastReceiver if someone passes one in.
        mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
    }
    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req,String tag) {
    req.setTag(tag);
    getRequestQueue().add(req);
}

public ImageLoader getImageLoader() {
    return mImageLoader;
}

public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}
}

Or maybe there is another issue in your code...

Mileage answered 15/1, 2018 at 9:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.