Retrofit + Okhttp.HTTP 504 Unused request (only in case of caching)

I used a POST request to access the server and did NOT send data to the server, but GET data from the server.

@POST("Index/getListShopsInfo") Observable<ShopList> getListShopsInfo();

and I want to add a local cache for my data.

public class CacheInterceptor implements Interceptor {

    private Context mContext;

    public CacheInterceptor(Context context) {
        mContext = context;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        boolean netAvailable = AppUtils.isNetworkAvailable(mContext);
        if (netAvailable) {
            request = request.newBuilder()
                .cacheControl(CacheControl.FORCE_NETWORK)
                .build();
        } else {
        request = request.newBuilder()
            .cacheControl(CacheControl.FORCE_CACHE)
            .build();
        }
        Response response = chain.proceed(request);
        if (netAvailable) {
            response = response.newBuilder()
                .removeHeader("Pragma")
                .header("Cache-Control", "public, max-age=" + 60 * 60)
                .build();
        } else {
            response = response.newBuilder()
                .removeHeader("Pragma")
                .header("Cache-Control", "public, only-if-cached, max-stale=" + 7 * 24 * 60 * 60)
                .build();
        }
        return response;
    }
}


File cacheFile = new File(AppUtils.getDiskCacheDir(getContext()), "responses");
Cache cache = new Cache(cacheFile, App.DEFAULT_DIR_CACHE);
OkHttpClient client = new OkHttpClient().newBuilder()
    .addInterceptor(new CacheInterceptor(getContext()))
    .retryOnConnectionFailure(true)
    .connectTimeout(15, TimeUnit.SECONDS)
    .cache(cache)
    .build();`

mRetrofit = new Retrofit.Builder()
    .baseUrl(App.LOGINURL)
    .client(client)
    .addConverterFactory(GsonConverterFactory.create())
    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
    .build();

When the network is connected, I can access the server, but when the network is not connected, my post will be intercepted. And he will write the wrong message:

HTTP 504 Unused request (only in case of caching)

I checked the local caching path and it has the “answers” ​​folder that I created and it has a file called “log” in it. When I open it in text format, it looks like this:

enter image description here

And I don’t know how to solve it.

Hope someone can help me!

+4
1

, .

FIRST. POST GET.

@GET("Index/getListShopsInfo") Call<ShopList> getListShopsInfo();

SECOND. Integer.

private final Interceptor REWRITE_RESPONSE_INTERCEPTOR = new Interceptor() {
    @Override
    public okhttp3.Response intercept(Chain chain) throws IOException {
        okhttp3.Response originalResponse = chain.proceed(chain.request());
        String cacheControl = originalResponse.header("Cache-Control");
        if (cacheControl == null || cacheControl.contains("no-store") || cacheControl.contains("no-cache") ||
                cacheControl.contains("must-revalidate") || cacheControl.contains("max-age=0")) {
            return originalResponse.newBuilder()
                    .removeHeader("Pragma")
                    .header("Cache-Control", "public, max-age=" + 5000)
                    .build();
        } else {
            return originalResponse;
        }
    }
};

private final Interceptor REWRITE_RESPONSE_INTERCEPTOR_OFFLINE = new Interceptor() {
    @Override
    public okhttp3.Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        if (!AppUtils.isNetworkAvailable(getContext())) {
            request = request.newBuilder()
                    .removeHeader("Pragma")
                    .header("Cache-Control", "public, only-if-cached")
                    .build();
        }
        return chain.proceed(request);
    }
};

THIRD.the okhttpClient.

File cacheFile = new File(AppUtils.getDiskCacheDir(getContext()), "FuckYouSecondDad");
    Cache cache = new Cache(cacheFile, App.DEFAULT_DIR_CACHE);
    OkHttpClient client = new OkHttpClient().newBuilder()
            .cache(cache)
            .addNetworkInterceptor(REWRITE_RESPONSE_INTERCEPTOR)
            .addInterceptor(REWRITE_RESPONSE_INTERCEPTOR_OFFLINE)
            .build();

FORTH..

mRetrofit = new Retrofit.Builder()
            .baseUrl(App.LOGINURL)
            .client(mClient)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();

, , . :

enter image description here

, :

.removeHeader("Pragma")

504.

+6

Source: https://habr.com/ru/post/1672816/


All Articles