2

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

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

活动类:

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;
    }

}

我还尝试okHttpRequestBuilder.cacheControl(CacheControl.FORCE_NETWORK);在 OKHTTP 堆栈类中查看它是否相关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;
    }
4

2 回答 2

1

我们可以像下面这样禁用 OKhhtp 的缓存:

OkHttpClient client = new OkHttpClient();
client.setCache(null);

这应该可以帮助您添加请求。

  Volley.newRequestQueue(context.getApplicationContext(), new OkHttpStack(client));

setCache() method will disable cache . I hope this was helpful. ThankYou

于 2016-03-29T05:30:47.537 回答
0

We can disable cache from Volley itself

reqObj.setShouldCache(false);

before add the request in the queue

于 2016-03-29T13:17:30.480 回答