Volley使用StringRequest在第一次调用后不调用getParams来发送POST请求参数

时间:2018-01-06 11:18:45

标签: android caching post android-volley

我遇到的问题是我的POST请求参数在第一次之后没有进入服务器。我知道Volley正在使用缓存机制进行响应,但在我的情况下,我的请求参数值可以在运行时更改,因为我在Recyclerview中使用了分页。

所以我的问题是我如何每次都发送Post请求参数,并且不会松开volley的缓存机制。

我尝试使用下面的内容并完成我的工作(每次都调用getParams())..但它会丢失缓存响应,我不想要它。

requestQueue.getCache().clear();

stringRequest.setShouldCache(false);

也有搜索谷歌及以下链接,但无法找到任何适当的解决方案。以下是SO链接

以下是我的代码:

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

使用以下Volley Dependency。

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

如果需要更多信息,请告诉我。提前致谢。您的努力将受到赞赏。

1 个答案:

答案 0 :(得分:1)

你检查过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);
    }
}
}

或许你的代码中还有另一个问题......