android - Retrofit response : Cache Control. 是什么意思?

标签 android caching picasso retrofit2

我使用 retrofit 和 picasso 库。 Picasso 自己管理缓存。但是当我查看 logcat 时,我看到下面的日志。这是什么意思? Web 服务和后端没有正确发送缓存信息吗?我该如何解决这个问题才能正确制作 Retrofit 缓存。那么,这个数据有意义吗?

Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Content-Type: application/json
Date: Wed, 14 Dec 2016 07:15:48 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
OkHttp-Received-Millis: 1481597410805
OkHttp-Response-Source: NETWORK 200
OkHttp-Selected-Protocol: http/1.1
OkHttp-Sent-Millis: 1481597409021
Pragma: no-cache
Server: Apache
Transfer-Encoding: chunked

最佳答案

您可以使用拦截器将缓存选项设置为 OkHttp。

OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new CachingControlInterceptor());
Retrofit restAdapter = new Retrofit.Builder()
        .client(client)
        .baseUrl(Constants.BASE_URL)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();

CachingControlInterceptor 是:

public class CachingControlInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

    // Add Cache Control only for GET methods
    if (request.method().equals("GET")) {
        if (ConnectivityUtil.checkConnectivity(YaootaApplication.getContext())) {
            // 1 day
           request = request.newBuilder()
                    .header("Cache-Control", "only-if-cached")
                    .build();
        } else {
            // 4 weeks stale
           request = request.newBuilder()
                    .header("Cache-Control", "public, max-stale=2419200")
                    .build();
        }
    }

    Response originalResponse = chain.proceed(request);
    return originalResponse.newBuilder()
        .header("Cache-Control", "max-age=600")
        .build();
}
}

看这个: https://stackoverflow.com/a/34401686/850347

关于android - Retrofit response : Cache Control. 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41137244/

相关文章:

java - Camera.FaceDetectionListener 不工作

asp.net - 缩小缓存数据范围的 Datatable.Select() 最快替代方案?

silverlight - 确保从您的网站下载网页内容而不是使用缓存临时 Internet 文件的最佳方法是什么?

android - picasso 加载所有手机照片的缩略图

android - 如何在 Android Native Facebook 登录中指定隐私设置(公开、 friend 或只有我)?

android - OrmLite Android,将 foreignAutoRefresh 设置为 true 会导致加入(自加入)查询吗?

android - AppCompatActivity 按钮文本的字体样式已更改

objective-c - 使用 AFNetworking 的 setImageWithURL 时如何配置缓存

android - Picasso 2.5.2 图像未加载网络策略

android - 如何使用 Picasso 设置 @BindingAdapter?