Android Volley 无法为使用 OkHttp3 作为其传输的特定请求禁用缓存
Android Volley unable to disable cache for a specific request that uses OkHttp3 as its transport
我已经尝试了几种方法来为特定的 volley 请求禁用缓存,但该应用程序始终使用缓存的数据。我的 Feed header 中有 header("Cache-Control: no-store,no-cache, must-revalidate");
。我从 Android Studio 启动了多个测试,并且提要的时间戳在 Logcat 中仍然相同。
我已经添加了 stringRequest.setShouldCache(false);
并在发出请求之前清除了缓存 queue.getCache().clear();
,但是它不起作用。
Activity class:
private RequestQueue queue;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
*VolleyStringRequest is basically a class
* that extends StringRequest with some common methods like `getHeaders()`.
*/
VolleyStringRequest stringRequest = new VolleyStringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("note", response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
new VolleyErrorListener(context, error);
}
}
);
queue = CustomVolleyRequestQueue.getInstance(this.getApplicationContext())
.getRequestQueue();
/**************HERE***************/
queue.getCache().clear();
stringRequest.setShouldCache(false);
queue.add(stringRequest);
}
CustomVolleyRequestQueue Class:
public class CustomVolleyRequestQueue {
private static CustomVolleyRequestQueue mInstance;
private static Context mCtx;
private RequestQueue mRequestQueue;
private CustomVolleyRequestQueue(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
}
public static synchronized CustomVolleyRequestQueue getInstance(Context context) {
if (mInstance == null) {
mInstance = new CustomVolleyRequestQueue(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new OkHttpStack());
mRequestQueue = new RequestQueue(cache, network);
// Don't forget to start the volley request queue
mRequestQueue.start();
}
return mRequestQueue;
}
}
我也在 OKHTTP 堆栈 class 中尝试了 okHttpRequestBuilder.cacheControl(CacheControl.FORCE_NETWORK);
以查看它是否与 OkHttp3
有关,但没有什么区别:
public class OkHttpStack implements HttpStack {
public OkHttpStack() {
}
@Override
public HttpResponse performRequest(com.android.volley.Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
int timeoutMs = request.getTimeoutMs();
clientBuilder.connectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
clientBuilder.readTimeout(timeoutMs, TimeUnit.MILLISECONDS);
clientBuilder.writeTimeout(timeoutMs, TimeUnit.MILLISECONDS);
okhttp3.Request.Builder okHttpRequestBuilder = new okhttp3.Request.Builder();
okHttpRequestBuilder.url(request.getUrl());
/****************HERE*****************/
okHttpRequestBuilder.cacheControl(CacheControl.FORCE_NETWORK);
Map<String, String> headers = request.getHeaders();
for(final String name : headers.keySet()) {
okHttpRequestBuilder.addHeader(name, headers.get(name));
}
setConnectionParametersForRequest(okHttpRequestBuilder, request);
OkHttpClient client = clientBuilder.build();
okhttp3.Request okHttpRequest = okHttpRequestBuilder.build();
Call okHttpCall = client.newCall(okHttpRequest);
Response okHttpResponse = okHttpCall.execute();
StatusLine responseStatus = new BasicStatusLine(parseProtocol(okHttpResponse.protocol()), okHttpResponse.code(), okHttpResponse.message());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromOkHttpResponse(okHttpResponse));
Headers responseHeaders = okHttpResponse.headers();
for(int i = 0, len = responseHeaders.size(); i < len; i++) {
final String name = responseHeaders.name(i), value = responseHeaders.value(i);
if (name != null) {
response.addHeader(new BasicHeader(name, value));
}
}
return response;
}
我们可以像下面这样禁用OKhhtp的缓存:
OkHttpClient client = new OkHttpClient();
client.setCache(null);
并且这应该可以帮助您添加请求。
Volley.newRequestQueue(context.getApplicationContext(), new OkHttpStack(client));
setCache() 方法将禁用缓存。我希望这可以帮到你。谢谢
我们可以禁用 Volley 本身的缓存
reqObj.setShouldCache(false);
在队列中添加请求之前
我已经尝试了几种方法来为特定的 volley 请求禁用缓存,但该应用程序始终使用缓存的数据。我的 Feed header 中有 header("Cache-Control: no-store,no-cache, must-revalidate");
。我从 Android Studio 启动了多个测试,并且提要的时间戳在 Logcat 中仍然相同。
我已经添加了 stringRequest.setShouldCache(false);
并在发出请求之前清除了缓存 queue.getCache().clear();
,但是它不起作用。
Activity class:
private RequestQueue queue;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
*VolleyStringRequest is basically a class
* that extends StringRequest with some common methods like `getHeaders()`.
*/
VolleyStringRequest stringRequest = new VolleyStringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("note", response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
new VolleyErrorListener(context, error);
}
}
);
queue = CustomVolleyRequestQueue.getInstance(this.getApplicationContext())
.getRequestQueue();
/**************HERE***************/
queue.getCache().clear();
stringRequest.setShouldCache(false);
queue.add(stringRequest);
}
CustomVolleyRequestQueue Class:
public class CustomVolleyRequestQueue {
private static CustomVolleyRequestQueue mInstance;
private static Context mCtx;
private RequestQueue mRequestQueue;
private CustomVolleyRequestQueue(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
}
public static synchronized CustomVolleyRequestQueue getInstance(Context context) {
if (mInstance == null) {
mInstance = new CustomVolleyRequestQueue(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new OkHttpStack());
mRequestQueue = new RequestQueue(cache, network);
// Don't forget to start the volley request queue
mRequestQueue.start();
}
return mRequestQueue;
}
}
我也在 OKHTTP 堆栈 class 中尝试了 okHttpRequestBuilder.cacheControl(CacheControl.FORCE_NETWORK);
以查看它是否与 OkHttp3
有关,但没有什么区别:
public class OkHttpStack implements HttpStack {
public OkHttpStack() {
}
@Override
public HttpResponse performRequest(com.android.volley.Request<?> request, Map<String, String> additionalHeaders)
throws IOException, AuthFailureError {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
int timeoutMs = request.getTimeoutMs();
clientBuilder.connectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
clientBuilder.readTimeout(timeoutMs, TimeUnit.MILLISECONDS);
clientBuilder.writeTimeout(timeoutMs, TimeUnit.MILLISECONDS);
okhttp3.Request.Builder okHttpRequestBuilder = new okhttp3.Request.Builder();
okHttpRequestBuilder.url(request.getUrl());
/****************HERE*****************/
okHttpRequestBuilder.cacheControl(CacheControl.FORCE_NETWORK);
Map<String, String> headers = request.getHeaders();
for(final String name : headers.keySet()) {
okHttpRequestBuilder.addHeader(name, headers.get(name));
}
setConnectionParametersForRequest(okHttpRequestBuilder, request);
OkHttpClient client = clientBuilder.build();
okhttp3.Request okHttpRequest = okHttpRequestBuilder.build();
Call okHttpCall = client.newCall(okHttpRequest);
Response okHttpResponse = okHttpCall.execute();
StatusLine responseStatus = new BasicStatusLine(parseProtocol(okHttpResponse.protocol()), okHttpResponse.code(), okHttpResponse.message());
BasicHttpResponse response = new BasicHttpResponse(responseStatus);
response.setEntity(entityFromOkHttpResponse(okHttpResponse));
Headers responseHeaders = okHttpResponse.headers();
for(int i = 0, len = responseHeaders.size(); i < len; i++) {
final String name = responseHeaders.name(i), value = responseHeaders.value(i);
if (name != null) {
response.addHeader(new BasicHeader(name, value));
}
}
return response;
}
我们可以像下面这样禁用OKhhtp的缓存:
OkHttpClient client = new OkHttpClient();
client.setCache(null);
并且这应该可以帮助您添加请求。
Volley.newRequestQueue(context.getApplicationContext(), new OkHttpStack(client));
setCache() 方法将禁用缓存。我希望这可以帮到你。谢谢
我们可以禁用 Volley 本身的缓存
reqObj.setShouldCache(false);
在队列中添加请求之前