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:

And I don’t know how to solve it.
Hope someone can help me!