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

标签 android caching android-volley okhttp

我想尝试 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;
    }
}

https://gist.github.com/bryanstern/4e8f1cb5a8e14c202750 中引用了哪个OkHttpClient

最佳答案

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

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

然后,我们可以保留一份由 Volley 维护的 HTTP 缓存。

改进:

我想尝试回答索蒂的问题。

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

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

是的,我将 Volley 缓存用于我的 HTTP 缓存而不是 OkHttp 缓存; 它对我很有用。

3 开箱即用的默认行为是什么?

对于 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.推荐的行为是什么以及如何实现?

作为 this帖子说:

Volley takes care of requesting, loading, caching, threading, synchronization and more. It’s ready to deal with JSON, images, caching, raw text and allow some customization.

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

例如,我们可以像这样对缓存进行更多控制 Android Volley + JSONObjectRequest Caching .

关于android - 使用 Volley 和 OkHttp 时如何配置 Http 缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30025002/

相关文章:

Android 特定的 MIME 类型列表?

java - 在 fragment 中追加文本的方法导致空指针

android - 如何解决android中的SAXParseException?

mysql - 我可以在我的网站前面放一个缓存服务器吗?

ruby-on-rails - 在 rails 中缓存 http 调用结果

android - 手动添加 Volley 到 android studio 项目,gradle offline

android - 下拉抽屉布局 Android

ruby-on-rails-3 - 文章更新后如何使主页缓存过期?

kotlin - 你能在 Kotlin 的 stringRequest 变量之外获取 Volley 的响应数据吗?

android - 使用 Kotlin 将参数放入请求 Volley android 中