【问题标题】:OkHttp + Retrofit offline cache not workingOkHttp +改造离线缓存不起作用
【发布时间】:2015-06-01 17:13:58
【问题描述】:

我正在从两台服务器请求 json 数据。 OkHttp 不缓存来自第一台服务器的响应,来自第二台服务器的响应正在正确缓存。

来自第一台服务器的响应,缓存由于某种原因无法正常工作:

<--- HTTP 200
Cache-Control: public, max-age=2230
Content-Type: application/json; charset=utf-8
Expires: Sat, 28 Mar 2015 11:34:13 GMT
Last-Modified: Sat, 28 Mar 2015 10:34:13 GMT
Vary: *
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Date: Sat, 28 Mar 2015 10:57:01 GMT
Content-Length: 1381
OkHttp-Selected-Protocol: http/1.1
OkHttp-Sent-Millis: 1427540192625
OkHttp-Received-Millis: 1427540192940
<--- END HTTP (1381-byte body)

设备离线时第一台服务器的响应:

---> HTTP GET
Cache-Control: public, only-if-cached, max-stale=2419200
---> END HTTP (no body)
<--- HTTP 504
<--- END HTTP (0-byte body)

来自第二台服务器的响应,缓存有效:

<--- HTTP 200
Content-Type: application/json; charset=utf-8
Date: Sat, 28 Mar 2015 11:19:51 GMT
Server: nginx/1.6.2
X-Berry-Env: p4
X-Berry-Version: 2.10.0.96e8b7c
X-Powered-By: Express
Connection: keep-alive
OkHttp-Selected-Protocol: http/1.1
OkHttp-Sent-Millis: 1427541591103
OkHttp-Received-Millis: 1427541591385
<--- END HTTP (92663-byte body)

设备离线时第二台服务器的响应:

<--- HTTP 200
Content-Type: application/json; charset=utf-8
Date: Sat, 28 Mar 2015 11:19:51 GMT
Server: nginx/1.6.2
X-Berry-Env: p4
X-Berry-Version: 2.10.0.96e8b7c
X-Powered-By: Express
Connection: keep-alive
OkHttp-Selected-Protocol: http/1.1
OkHttp-Sent-Millis: 1427541591103
OkHttp-Received-Millis: 1427541591385
Warning: 110 HttpURLConnection "Response is stale"
Validating map...
<--- END HTTP (92663-byte body)

我关注了this answer,这是我的休息适配器:

protected RestAdapter getRestAdapter(final Context context) {
    if (restAdapter == null) {
        Gson gson = new GsonBuilder()
                .excludeFieldsWithoutExposeAnnotation()
                .create();

        int cacheSize = 10 * 1024 * 1024;
        Cache cache = new Cache(context.getCacheDir(), cacheSize);
        OkHttpClient client = new OkHttpClient();
        client.setCache(cache);

        restAdapter = new RestAdapter.Builder()
                .setConverter(new GsonConverter(gson))
                .setEndpoint(API_URL)
                .setClient(new OkClient(client))
                .setRequestInterceptor(new RequestInterceptor() {
                    @Override
                    public void intercept(RequestFacade request) {
                        if (Utils.isOnline(context)) {
                            int maxAge = 60;
                            request.addHeader("Cache-Control", "public, max-age=" + maxAge);
                        } else {
                            int maxStale = 60 * 60 * 24 * 28;
                            request.addHeader("Cache-Control",
                                    "public, only-if-cached, max-stale=" + maxStale);
                        }
                    }
                })
                .build();
        restAdapter.setLogLevel(RestAdapter.LogLevel.FULL);
    }

    return restAdapter;
}

【问题讨论】:

    标签: android caching retrofit okhttp


    【解决方案1】:

    我已经通过使用网络拦截器解决了这个问题。

    【讨论】:

      【解决方案2】:

      除了 Ivan 的回答之外,OkHttp3 缓存不适用于发布请求。 要缓存发布请求,您必须使用其他机制。

      【讨论】:

        猜你喜欢
        • 2019-03-25
        • 1970-01-01
        • 2015-09-28
        • 2017-02-03
        • 2012-07-07
        • 2016-02-18
        • 2015-10-07
        • 2018-02-04
        • 2023-03-16
        相关资源
        最近更新 更多