504不满意的请求 - 改造

时间:2017-04-09 07:02:13

标签: java android caching retrofit2 okhttp3

我写了以下代码来获取&将MarvelAPI数据缓存到我的设备上。 缓存文件夹包含缓存dat a但是Retrofit NEVER在离线时读取缓存,而是提供 504不满意的请求错误

我不确定我做错了什么。

网址: http://gateway.marvel.com/v1/public/comics?apikey=publickey&hash=04bd8e724fe4a647063489f19822cbcss3&ts=1491690808&limit=6&offset=6

private static final String HTTP_CACHE_PATH = "http-cache";
private static final String CACHE_CONTROL = "Cache-Control";
private static final String PRAGMA = "Pragma";


@Provides
@NetworkComponentScope
public Retrofit providesRetrofit(OkHttpClient okHttpClient, GsonConverterFactory gsonConverterFactory, String baseUrl) {
    return new Retrofit.Builder()
            .client(okHttpClient)
            .addConverterFactory(gsonConverterFactory)
            .baseUrl(baseUrl)
            .build();
}

@Provides
@NetworkComponentScope
public OkHttpClient providesOkHttpClient(Context context, OkHttpClient.Builder builder, HttpLoggingInterceptor okhttpLoggingInterceptor, Cache cache) {
    builder.cache(cache);
    builder.addNetworkInterceptor(provideCacheInterceptor(5000));
    builder.addInterceptor(provideOfflineCacheInterceptor(context, 5000));
    if(BuildConfig.DEBUG) {
        builder.addInterceptor(okhttpLoggingInterceptor);
    }

    return builder.build();
}

@Provides
@NetworkComponentScope
@PicassoLoggingInterceptor
public OkHttpClient providesOkHttpClientForPicasso(OkHttpClient.Builder builder, HttpLoggingInterceptor okhttpLoggingInterceptor) {
    if(BuildConfig.DEBUG) {
        builder.addInterceptor(okhttpLoggingInterceptor);
    }

    return builder.build();
}

@Provides
public OkHttpClient.Builder provideOkHttpClientBuilder() {
    OkHttpClient.Builder okhttpClientBuilder = new OkHttpClient().newBuilder();
    okhttpClientBuilder.connectTimeout(25, TimeUnit.SECONDS);
    okhttpClientBuilder.readTimeout(25, TimeUnit.SECONDS);
    return okhttpClientBuilder;
}

@Provides
@NetworkComponentScope
public Cache providesCache(Context context) {
    File httpCacheDirectory = new File(context.getCacheDir(), "marvel_responses");
    int cacheSize = 10 * 1024 * 1024; // 10 MB
    return new Cache(httpCacheDirectory, cacheSize);
}

@Provides
public HttpLoggingInterceptor providesOkhttpLoggingInterceptor() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
    return interceptor;
}


private Interceptor provideCacheInterceptor(final int maxAgeMin) {
    return new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Response response = chain.proceed(chain.request());

            CacheControl cacheControl = new CacheControl.Builder()
                    .maxAge(maxAgeMin, TimeUnit.MINUTES)
                    .build();

            return response.newBuilder()
                    .removeHeader(PRAGMA)
                    .removeHeader(CACHE_CONTROL)
                    .header(CACHE_CONTROL, cacheControl.toString())
                    .build();            }
    };
}

public Interceptor provideOfflineCacheInterceptor(final Context context, final int maxStaleDay) {
    return new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {

            Request request = chain.request();

            if (!Utils.isNetworkAvailable(context)) {
                CacheControl cacheControl = new CacheControl.Builder()
                        .maxStale(maxStaleDay, TimeUnit.DAYS)
                        .build();

                request = request.newBuilder()
                        .cacheControl(cacheControl)
                        .build();
            }

            return chain.proceed(request);
        }
    };
}



compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
compile "com.squareup.retrofit2:adapter-rxjava:2.2.0"
compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'

0 个答案:

没有答案