当使用Volley和OkHttp时如何配置Http缓存?

20

我想尝试将Volley与OkHttp结合使用,但是Volley缓存系统和OkHttp都依赖于HTTP规范中定义的HTTP缓存。那么如何禁用OkHttp的缓存以保留一个HTTP缓存的副本?

编辑:我已经做了什么

public class VolleyUtil {
    // http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/
    private volatile static RequestQueue sRequestQueue;

    /** get the single instance of RequestQueue **/
    public static RequestQueue getQueue(Context context) {
        if (sRequestQueue == null) {
            synchronized (VolleyUtil.class) {
                if (sRequestQueue == null) {
                    OkHttpClient client = new OkHttpClient();
                    client.networkInterceptors().add(new StethoInterceptor());
                    client.setCache(null);
                    sRequestQueue = Volley.newRequestQueue(context.getApplicationContext(), new OkHttpStack(client));
                    VolleyLog.DEBUG = true;
                }
            }
        }
        return sRequestQueue;
    }
}

哪个OkHttpClient是从https://gist.github.com/bryanstern/4e8f1cb5a8e14c202750引用的?


你能展示一下你所做的吗? - Soham
@Soham,感谢您的回复,我已经重新编辑了我的问题,谢谢。 - Xiaozou
2个回答

17

OkHttp是一种像HttpUrlConnection一样实现HTTP缓存的HTTP客户端,我们可以像下面这样禁用OkHttp的缓存:

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

那么,我们可以通过Volley维护一个HTTP缓存的副本。

改进:

我想尝试回答Sotti的问题。

1. 在使用Volley和OkHttp时,什么是良好的缓存设置?

在我的项目中,我跨所有RESTful API使用一个Volley requestQueue实例,并且OkHttp作为Volley的传输层,如下所示。

public class VolleyUtil {
// http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/
private volatile static RequestQueue sRequestQueue;

/** get the single instance of RequestQueue **/
public static RequestQueue getQueue(Context context) {
    if (sRequestQueue == null) {
        synchronized (VolleyUtil.class) {
            if (sRequestQueue == null) {
                OkHttpClient client = new OkHttpClient();
                client.setCache(null);
                sRequestQueue = Volley.newRequestQueue(context.getApplicationContext(), new OkHttpStack(client));
                VolleyLog.DEBUG = true;
            }
        }
    }
    return sRequestQueue;
}}

我们应该依赖于Volley还是OkHttp缓存呢?

是的,我正在使用Volley缓存而非OkHttp缓存;对我来说效果很好。

默认情况下的行为是什么?

对于Volley:

它会自动为您创建一个名为“volley”的默认缓存目录。

/** Default on-disk cache directory. */
private static final String DEFAULT_CACHE_DIR = "volley";


public static RequestQueue newRequestQueue(Context context, HttpStack stack, int maxDiskCacheBytes) {
    File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
    ……
}

对于OkHttp:

我在源代码中找不到默认的缓存,但我们可以像这篇文章http://blog.denevell.org/android-okhttp-retrofit-using-cache.html中所示设置响应缓存。

4. 推荐的行为是什么,如何实现?

正如这篇文章所说:

Volley负责请求、加载、缓存、线程、同步等。它已准备好处理JSON、图像、缓存、纯文本,并允许一些自定义。

我喜欢使用Volley HTTP缓存,因为它易于定制。

例如,我们可以通过Android Volley + JSONObjectRequest Caching这样具有更多控制权的方式来控制缓存。


4

OkHttp 优雅地忽略缓存的方法是:

request.setCacheControl(CacheControl.FORCE_NETWORK);

这仅适用于在此处讨论的特定请求。https://github.com/square/okhttp/issues/1496 - Xiaozou

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接