Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用OKHttp进行改造如何在脱机时使用缓存数据_Android_Caching_Gzip_Retrofit_Okhttp - Fatal编程技术网

Android 使用OKHttp进行改造如何在脱机时使用缓存数据

Android 使用OKHttp进行改造如何在脱机时使用缓存数据,android,caching,gzip,retrofit,okhttp,Android,Caching,Gzip,Retrofit,Okhttp,我想在没有互联网的情况下改用OkHttp使用缓存 我准备OkHttpClient如下: RestAdapter.Builder builder= new RestAdapter.Builder() .setRequestInterceptor(new RequestInterceptor() { @Override public void intercept(RequestFacade request) {

我想在没有互联网的情况下改用OkHttp使用缓存

我准备OkHttpClient如下:

    RestAdapter.Builder builder= new RestAdapter.Builder()
       .setRequestInterceptor(new RequestInterceptor() {
            @Override
            public void intercept(RequestFacade request) {
                request.addHeader("Accept", "application/json;versions=1");
                if (MyApplicationUtils.isNetworkAvaliable(context)) {
                    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);
                }
            }
    });
   Cache cache = null;
    try {
        cache = new Cache(httpCacheDirectory, 10 * 1024 * 1024);
    } catch (IOException e) {
        Log.e("OKHttp", "Could not create http cache", e);
    }

    OkHttpClient okHttpClient = new OkHttpClient();
    if (cache != null) {
        okHttpClient.setCache(cache);
    }
并按如下方式设置缓存:

    RestAdapter.Builder builder= new RestAdapter.Builder()
       .setRequestInterceptor(new RequestInterceptor() {
            @Override
            public void intercept(RequestFacade request) {
                request.addHeader("Accept", "application/json;versions=1");
                if (MyApplicationUtils.isNetworkAvaliable(context)) {
                    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);
                }
            }
    });
   Cache cache = null;
    try {
        cache = new Cache(httpCacheDirectory, 10 * 1024 * 1024);
    } catch (IOException e) {
        Log.e("OKHttp", "Could not create http cache", e);
    }

    OkHttpClient okHttpClient = new OkHttpClient();
    if (cache != null) {
        okHttpClient.setCache(cache);
    }
我在根设备上进行了检查,在缓存目录中保存了带有“响应头”和Gzip文件的文件


但我并没有从脱机的改装缓存中得到正确的答案,尽管在GZip文件中,我的正确答案是编码的。那么,我如何才能进行改装?我可以读取GZip文件,他如何知道应该是哪个文件(因为我有一些其他响应的文件)?

我的公司有一个类似的问题:)

问题出在服务器端。在serwer的回复中,我有:

Pragma: no-cache
所以当我把它移除后,一切都开始工作了。在我删除它之前,我一直收到这样的异常:
504不可满足的请求(仅在缓存时)

好的,那么我这边的实现看起来如何

    OkHttpClient okHttpClient = new OkHttpClient();

    File httpCacheDirectory = new File(appContext.getCacheDir(), "responses");

    Cache cache = new Cache(httpCacheDirectory, maxSizeInBytes);
    okHttpClient.setCache(cache);

    OkClient okClient = new OkClient(okHttpClient);

    RestAdapter.Builder builder = new RestAdapter.Builder();
    builder.setEndpoint(endpoint);
    builder.setClient(okClient);
如果您在测试中遇到问题,问题出在哪一边(服务器或应用程序)。您可以使用这种安全设置从服务器接收的头

private static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Response originalResponse = chain.proceed(chain.request());
        return originalResponse.newBuilder()
                               .removeHeader("Pragma")
                               .header("Cache-Control",
                                       String.format("max-age=%d", 60))
                               .build();
    }
};
简单地加上:

okHttpClient.networkInterceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR);
多亏了这一点,正如您所看到的,我能够删除测试时间的
Pragma:no cache

另外,我建议您阅读
缓存控制
标题:

,

其他有用的链接:


你就是那个男人!!你不仅救了我一天,还救了我两天。