gpt4 book ai didi

Android Volley 无法为使用 OkHttp3 作为其传输的特定请求禁用缓存

转载 作者:行者123 更新时间:2023-11-29 01:22:01 24 4
gpt4 key购买 nike

我已经尝试了几种方法来为特定的 volley 请求禁用缓存,但该应用程序始终使用缓存的数据。我的提要 header 中有 header("Cache-Control: no-store,no-cache, must-revalidate");。我从 Android Studio 启动了多个测试,提要的时间戳在 Logcat 中仍然相同。

我已经添加了 stringRequest.setShouldCache(false); 并在发出请求之前清除缓存 queue.getCache().clear(); ,但它不是'工作。

Activity 类:

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 类:

 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 堆栈类中尝试了 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() 方法将禁用缓存。我希望这可以帮到你。谢谢

关于Android Volley 无法为使用 OkHttp3 作为其传输的特定请求禁用缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36275996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com