我正在使用 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/