我也在尝试通过改造以脱机模式访问我的数据,但这没有发生。
代码没有错,但是没有用。
两个WiFi状态的日志数据都相同。
OkHttpClient client = new OkHttpClient
.Builder()
.addInterceptor(logging)
.addInterceptor(new Interceptor() {
@Override public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (isNetworkAvailable(context,ConnectivityManager.TYPE_WIFI)) {
request = request.newBuilder().header("Cache-Control", "public, max-age=" + 60).build();
Log.d("data",request.toString());
} else {
request = request.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 7).build();
Log.d("data1",request.toString());
}
return chain.proceed(request);
}
})
.cache(new Cache(httpCacheDirectory, 10 * 1024 * 1024)) // 10 MB
.build();
答案 0 :(得分:0)
这应该对您有用。
OkHttpClient okHttpClient = new OkHttpClient()
.newBuilder()
.cache(new Cache(getyourapplicationcontext.getCacheDir(), 10 * 1024 *1024))
.addInterceptor(chain -> {
Request request = chain.request();
if (NetworkUtil.isDeviceConnectedToInternet(getyourapplicationcontext)) {
request = request.newBuilder().header("Cache-Control", "public, max-age=" + 60).build();
} else {
request = request.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 7).build();
}
return chain.proceed(request);
})
.build();``