OkHttp没有在Android上缓存响应

OkHttp没有在Android上缓存响应,android,retrofit,square,http-caching,okhttp,Android,Retrofit,Square,Http Caching,Okhttp,我正在使用改型1.8.0、okhttp 2.1.0和okhttp urlconnection 2.1.0,我希望缓存从服务器获得的响应,并在没有internet连接时使用它们,这是我的代码: RequestInterceptor requestInterceptor = new RequestInterceptor() { @Override public void intercept(RequestFacade request) { request.addH

我正在使用改型1.8.0、okhttp 2.1.0和okhttp urlconnection 2.1.0,我希望缓存从服务器获得的响应,并在没有internet连接时使用它们,这是我的代码:

RequestInterceptor requestInterceptor = new RequestInterceptor() {

    @Override
    public void intercept(RequestFacade request) {

        request.addHeader("Accept", "application/json");
        if (Utils.isNetworkAvailable(HancoApplication.this)) {

            int maxAge = 60; // read from cache for 1 minute
            request.addHeader("Cache-Control", "public, max-age=" + maxAge);
        } else {

            int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
            request.addHeader("Cache-Control","public, only-if-cached, max-stale=" + maxStale);
        }
    }
};
OkHttpClient okHttpClient = new OkHttpClient();
File cacheDir = new File(getCacheDir(), "responseCache");
Cache cache = null;
try {

    cache = new Cache(cacheDir, 1024 * 1024 * 12);
    okHttpClient.setCache(cache);
} catch (IOException e) {

    e.printStackTrace();
}
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(Const.API_URL).setClient(new OkClient(okHttpClient)).setRequestInterceptor(requestInterceptor).build();
mWebApi = restAdapter.create(WebApi.class);
当我在手机上没有互联网的情况下执行相同的请求时,它会返回以下内容: HTTP/1.1 504不可满足的请求(仅当缓存时) OkHttp响应源:无


我不知道我的代码有什么问题,互联网上的所有资源都使用相同的代码

您的HTTP服务器返回什么缓存头?这些是我从服务器缓存控件获得的所有头:“max age=604800,public,必须重新验证”连接:keep-alive-Content-Type:application/json-Date:Thu,2014年11月27日08:14:46 GMT OkHttp收到Millis:1417076086482 OkHttp响应来源:网络200 OkHttp选定协议:http/1.1 OkHttp发送Millis:1417076085965服务器:牛仔传输编码:分块通过:1.1 vegur X-Apariy-Ratelimit-Limit:120 X-Apariy-Ratelimit-Le剩馀:119 X-Apariy-Transaction-Id:5476DD764CE90200A3Be16Your响应说
必须重新验证
(因此服务器要求您调用它),而您的请求说
只有在缓存的情况下(因此客户端拒绝调用服务器)。这些是互不兼容的。(OkHttp有一个忽略服务器头的功能请求;这将在未来的版本中发布)。我更改了服务器的响应以匹配中的示例,服务器现在返回:Cache Control:public,max age=60,当我关闭wifi再次执行请求时,会引发未知的hostException。我做错了什么?