使用retrofit2和OkHttp3时出现此错误.无法解析主机"<host-name>":没有与主机名关联的地址

and*_*ann 15 android offline-caching okhttp retrofit2 okhttp3

我正在使用改造2和OkHttp3从服务器请求数据.我刚刚添加了一个离线缓存代码,但它没有按预期工作.我收到错误"无法解析主机"<>":没有与主机名关联的地址."

当它试图从缓存中获取检索数据时(没有互联网连接时)会发生这种情况.下面是一段代码片段.

public static Interceptor provideCacheInterceptor() {
    return new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Response response = chain.proceed(chain.request());

            // re-write response header to force use of cache
            CacheControl cacheControl = new CacheControl.Builder()
                    .maxAge(2, TimeUnit.MINUTES)
                    .build();

            return response.newBuilder()
                    .header(CACHE_CONTROL, cacheControl.toString())
                    .build();
        }
    };
}

public static Interceptor provideOfflineCacheInterceptor() {
    return new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            if (!hasNetwork) {
                CacheControl cacheControl = new CacheControl.Builder()
                        .onlyIfCached()
                        .maxStale(7, TimeUnit.DAYS)
                        .build();

                request = request.newBuilder()
                        .removeHeader("Pragma")
                        .cacheControl(cacheControl)
                        .build();
            }

            return chain.proceed(request);
        }
    };
}

private static Cache provideCache() {
    Cache cache = null;
    try {
        cache = new Cache(new File(AdeptAndroid.getInstance().getCacheDir(), "http-cache"),
                10 * 1024 * 1024); // 10 MB
    } catch (Exception e) {
        Log.d("Test", "Could not create Cache!");
    }
    return cache;
}
Run Code Online (Sandbox Code Playgroud)

最后,结合所有这些的方法就在这里.

private static OkHttpClient provideOkHttpClient() {
    return new OkHttpClient.Builder()
            .addInterceptor(provideHttpLoggingInterceptor())
            .addInterceptor(provideOfflineCacheInterceptor())
            .addNetworkInterceptor(provideCacheInterceptor())
            .cache(provideCache())
            .build();
}
Run Code Online (Sandbox Code Playgroud)

Ibr*_*him 1

您的服务器响应有一个“Pragma:no-cache”标头。您应该在响应拦截器而不是请求拦截器中删除此标头。

在当前代码中,您已将其从请求拦截器中删除。

你的provideCacheInterceptor()应该看起来像这样:

public static Interceptor provideCacheInterceptor() {
    return new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Response response = chain.proceed(chain.request());

            // re-write response header to force use of cache
            CacheControl cacheControl = new CacheControl.Builder()
                   .maxAge(2, TimeUnit.MINUTES)
                   .build();

           return response.newBuilder()
                    .header(CACHE_CONTROL, cacheControl.toString())
                    .removeHeader("Pragma")
                    .build();
        }
    };
}
Run Code Online (Sandbox Code Playgroud)