我使用的是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();
但结果是一样的。
如何从响应中强制禁用缓存?
非常感谢您的建议。
发布于 2016-06-13 21:32:52
request.setShouldCache(false);
似乎不足以满足GET请求。但是,在添加到队列之前清除缓存似乎会有所帮助
myRequestQueue.getCache().clear();
在返回requestQueue
之前,我把它放在我的Volley singleton的getRequestQueue()
方法中。
发布于 2016-07-10 14:26:21
调用以下代码:
myRequestQueue.getCache().remove(url);
发布于 2016-01-17 16:01:03
要使用Volley而不使用响应缓存,而不是使用Volley.newRequestQueue(),可以创建自己的RequestQueue,如下所示:
HttpStack stack;
if (Build.VERSION.SDK_INT >= 9) {
stack = new HurlStack();
} else {
stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
}
Network network = new BasicNetwork(stack);
queue = new RequestQueue(new NoCache(), network);
关键是NoCache对象,它实现Volley Cache接口,但什么也不做。
的好处:如果你愿意,你也可以使用OkHttp (应用附带的)来实现HttpStack。这种方法的好处是,既然你的应用附带了OkHttp库,你就可以放心,你的HttpStack实现总是可以在所有的安卓版本上工作,因为你不依赖于平台的HttpStack实现。另外,OkHttp有各种各样的优点,比如拦截器机制和一个非常简单的应用程序接口。
https://stackoverflow.com/questions/34792156
复制相似问题