Monday, January 15, 2018

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

Leave a Comment

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.

1 Answers

Answers 1

Did you checked your Volley Singleton is correct or not ?

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 other in your code.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment