【问题标题】:Retrofit 2.0.2: how to cache GET request?改造 2.0.2:如何缓存 GET 请求?
【发布时间】:2016-11-24 22:20:50
【问题描述】:

我正在尝试缓存我的 get 请求。对于改造 2 看来我不必为此使用 OKHttp。那我该如何缓存呢?我正在尝试这样:

private static Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl(BASE_URL /*+ API*/)
            .cache(new Cache(App.sApp.getCacheDir(), 10 * 1024 * 1024)) // 10 MB
            .addInterceptor(new Interceptor() {
                @Override public Response intercept(Chain chain) throws IOException {
                    Request request = chain.request();
                    if (ApiUtils.isNetworkAvailable()) {
                        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);
                }
            })
            .addConverterFactory(GsonConverterFactory.create(getGson()));

但这并没有发生。

【问题讨论】:

    标签: android retrofit2


    【解决方案1】:

    我从 GDGAhmedab​​ad Repo 中的 Retrofit2 Demo 中找到 OkClientFactory

    也许对你有帮助。

    public class OkClientFactory {
        // Cache size for the OkHttpClient
    
        private static final int DISK_CACHE_SIZE = 50 * 1024 * 1024; // 50MB
    
        private OkClientFactory() {
        }
    
        @NonNull
        public static OkHttpClient provideOkHttpClient(Application app) {
            // Install an HTTP cache in the application cache directory.
            File cacheDir = new File(app.getCacheDir(), "http");
            Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
    
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .cache(cache);
            if (BuildConfig.DEBUG) {
                HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
                loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                builder.interceptors().add(loggingInterceptor);
            }
            return builder.build();
        } }
    

    现在你可以像这样使用:

    .client(App.getInstance().getOkHttpClient())
    

    更多信息请见here

    谢谢。

    【讨论】:

      【解决方案2】:

      您需要指定只缓存 GET 请求。

      public class CachingControlInterceptor implements Interceptor {
      @Override
      public Response intercept(Chain chain) throws IOException {
          Request request = chain.request();
      
          // Add Cache Control only for GET methods
          if (request.method().equals("GET")) {
              if (ConnectivityUtil.checkConnectivity(getContext())) {
                  // 1 day
                  request = request.newBuilder()
                      .header("Cache-Control", "only-if-cached")
                      .build();
              } else {
                  // 4 weeks stale
                  request = request.newBuilder()
                      .header("Cache-Control", "public, max-stale=2419200")
                      .build();
              }
          }
      
          Response originalResponse = chain.proceed(request);
          return originalResponse.newBuilder()
              .header("Cache-Control", "max-age=600")
              .build();
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-06-16
        • 2015-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-25
        • 2015-01-12
        • 2020-07-28
        • 2017-12-10
        相关资源
        最近更新 更多