Retrofit Offline cashing returns a null response.body()

你离开我真会死。 提交于 2019-12-02 02:58:37

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 );
            }
        };
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!