I tried this link and this link to construct an offline Retrofit cache.
The problem is that if I put the phone in Airplane mode, the Response.body() is always null.
try this code
int cacheSize = 10 * 1024 * 1024; // 10 MB
Cache cache = new Cache(new File(getApplication().getCacheDir(),"someIdentifier"), cacheSize);
Interceptor offlineCacheInterceptor = new Interceptor() {
@Override
public Response intercept (Chain chain) throws IOException {
Request request = chain.request();
if(!App.isNetworkAvailable()) {
CacheControl cacheControl = new CacheControl.Builder()
.maxStale(30, TimeUnit.DAYS)
.build();
request = request.newBuilder()
.cacheControl(cacheControl)
.build();
}
return chain.proceed( request );
}
};