Retrofit2.2.0 和 okhttp3.9.1 离线缓存不起作用,当我请求离线数据时会抛出异常,即 HTTP 504 Unsatisfiable Request (only-if-cached)。数据正在从互联网加载到设备中,但离线模式无法检索它。
以下代码描述了 API 接口。
public class TestApi {
private static TestService testService;
private static final String TEST_URL = "https://httpbin.org/";
public static TestService getTestService() {
if(testService == null) {
synchronized (TestApi.class) {
if(testService == null) {
testService = XApi.getInstance().getRetrofit(TEST_URL, true).create(TestService.class);
}
}
}
return testService;
}
}
Run Code Online (Sandbox Code Playgroud)
以下代码描述了 API 服务。
public interface TestService {
@GET("/cache/60")
Flowable<TestBean> getTestDate();
}
Run Code Online (Sandbox Code Playgroud)
下面的代码描述了缓存控制拦截器。
public class CachingControlInterceptor {
private static final int TIMEOUT_CONNECT = 60; //60 second …Run Code Online (Sandbox Code Playgroud)