Android : Volley 1. 0.18 如何禁用请求缓存?

标签 android rest caching android-volley

我正在使用 volley 库:

compile 'com.mcxiaoke.volley:library:1.0.18'

在http helper类中我有以下方法。

  public static JsonRequest createRequest(String responseType, int requestMethod, String scheme,
                                            String url, final String requestParams, final HttpResponseListener listener,
                                            Request.Priority priority) throws UnsupportedEncodingException {
        // Start to prepare request URL
        Uri.Builder builder = new Uri.Builder();
        builder.scheme(scheme).encodedAuthority(url);
        // GET REQUESTS - append the params into URL
        if (requestMethod == Request.Method.GET && requestParams != null) {
            boolean append = appendParamsToUrl(requestParams, builder);
            if(!append) return null;
        }
        url = URLDecoder.decode(builder.build().toString(), Constants.Request.DEFAULT_ENCODING);
        // Get response as JSON object
        JsonRequest request;
        if (responseType.equals(Constants.Request.JSON_OBJECT)) {
            // Prepare request and set the Callbacks
            request = new CustomJsonObjectRequest(requestMethod, url, requestParams,
                    priority, responseListener(listener), errorListener(listener), listener);
        }else { // Get response as JSON array of objects
            // Prepare request and set the Callbacks
            request = new CustomJsonArrayRequest(requestMethod, url, requestParams,
                    priority, responseArrayListener(listener), errorListener(listener), listener);
        }
        request.setTag(REQUEST_TAG);
        request.setShouldCache(false);
        return request;
    }

当我使用该选项时:

request.setShouldCache(false);

强制禁用缓存。

但是当我从 POSTMAN(用于 API 测试的 Chrome 扩展)收到服务器响应时,我得到的响应值与 Android 设备上的不同。

我也尝试使用:

queue.getCache().clear();

但结果相同。

如何强制禁用响应缓存?

非常感谢您的建议。

最佳答案

request.setShouldCache(false);

似乎不足以满足 GET 请求。 但是,在添加到队列之前清除缓存似乎有帮助

myRequestQueue.getCache().clear();

在返回 requestQueue 之前,我将其放入 Volley 单例的 getRequestQueue() 方法中。

关于Android : Volley 1. 0.18 如何禁用请求缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34792156/

相关文章:

android - 从 Assets 文件夹获取mp3文件列表并保存为共享首选项

api - 使用概念上可以包含其他资源的资源在Laravel 5.2中设计RESTful API

caching - 页大小和 block 大小

caching - WP7 HttpWebRequest 没有缓存

java - Libgdx Scene2D 表背景图像未显示

android - GradientDrawable.setStroke 宽度似乎无法正常工作

django - 获取 "curl: (92) HTTP/2 stream 1 was not closed cleanly: INTERNAL_ERROR (err 2)"

python - 为什么我的 LRU 缓存未命中相同的参数?

android - NoReadAccessException - 只允许从事件分派(dispatch)线程或内部读取操作进行读取访问

java - 如何识别请求类型是同步还是异步