美文网首页
使用Retrofit和Okhttp3实现网络缓存

使用Retrofit和Okhttp3实现网络缓存

作者: gzfgeh | 来源:发表于2016-06-24 21:22 被阅读423次
    公司需求:有网络请求最新数据,无网络请求缓存
    @Singletonpublic class FreyNewRetrofit {    
      private FreyNewService freyService;    
      private Interceptor interceptor = new Interceptor() {        
        @Override        
        public Response intercept(Chain chain) throws IOException {            
          Request request = chain.request();            
          if (!NetWorkUtils.isNetworkAvailable()){                
            request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
          }            
          Response response = chain.proceed(request);            
          if (NetWorkUtils.isNetworkAvailable()){                
            int maxAge = 0;                
            response.newBuilder().header("Cache-Control", "public, max-age=" + maxAge)                        
                .removeHeader("Pragma")                        
                .build();            
          }else{                
            int maxStale = 60* 60* 24 * 7;                
            response.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)                        
                .removeHeader("Pragma")                        
                .build();            
          }            
          return response;        
         }    
      };  
      
      @Inject    
      public FreyNewRetrofit() {        
        File cacheFile = new File(APP.getContext().getCacheDir(), "FreyCache");        
        Cache cache = new Cache(cacheFile, 1024 * 1024 * 100);        
        OkHttpClient okHttpClient = new OkHttpClient.Builder()                
          .addNetworkInterceptor(new StethoInterceptor())                
          .connectTimeout(10000, TimeUnit.MILLISECONDS)                
          .readTimeout(10000, TimeUnit.MILLISECONDS)                
          .addInterceptor(interceptor)                
          .cache(cache)                
          .build();   
         
        Retrofit retrofit = new Retrofit.Builder()                
          .baseUrl(UrlUtils.NEW_BASE_URL)                
          .addCallAdapterFactory(RxJavaCallAdapterFactory.create())                
          .addConverterFactory(GsonConverterFactory.create())                
          .client(okHttpClient)                
          .build();        
          freyService = retrofit.create(FreyNewService.class);    
         }    
          public FreyNewService getFreyService(){        
            return freyService;    
          }
      }
    

    有网没网都取缓存

    Interceptor interceptor = new Interceptor() { 
        @Override public Response intercept(Chain chain) throws IOException { 
          Request request = chain.request(); 
          Response response = chain.proceed(request);     
          String cacheControl = request.cacheControl().toString(); 
          if (TextUtils.isEmpty(cacheControl)) { 
              cacheControl = "public, max-age=60"; 
          } 
          return response.newBuilder() .header("Cache-Control", cacheControl) .removeHeader("Pragma") .build(); 
        } 
      };
    

    相关文章

      网友评论

          本文标题:使用Retrofit和Okhttp3实现网络缓存

          本文链接:https://www.haomeiwen.com/subject/ggaodttx.html