Reputation: 1423
I`m using retrofit_2 (beta4) with okhttp_3 libraries in network requests. And i need to cache response data for situations when network is off and application must show responce data from last identical request. All guides for solving this problem that i found were with okhttp lib (not okhttp_3). I tried to solve problem:
public class ApiFactory {
private static final int CONNECT_TIMEOUT = 45;
private static final int WRITE_TIMEOUT = 45;
private static final int READ_TIMEOUT = 45;
private static final long CACHE_SIZE = 10 * 1024 * 1024; // 10 MB
private static OkHttpClient.Builder clientBuilder;
static {
clientBuilder = new OkHttpClient
.Builder()
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
.cache(new Cache(MyApp.getInstance().getCacheDir(), CACHE_SIZE)) // 10 MB
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (MyApp.getInstance().isNetwConn()) {
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);
}
});
}
@NonNull
public static ApiRequestService getApiRequestService() {
return getRetrofitDefault().create(ApiRequestService.class);
}
@NonNull
private static Retrofit getRetrofitDefault() {
return new Retrofit.Builder()
.baseUrl(NetworkUrls.URL_MAIN)
.addConverterFactory(GsonConverterFactory.create())
.callbackExecutor(Executors.newFixedThreadPool(5))
.callbackExecutor(Executors.newCachedThreadPool())
.callbackExecutor(new Executor() {
private final Handler mHandler = new Handler(Looper.getMainLooper());
@Override
public void execute(Runnable command) {
mHandler.post(command);
}
})
.client(clientBuilder.build())
.build();
}
}
but this doesnt work. All requests work well when network is ON, but not return cache data when network is OFF. Please, help to solve this problem.
Upvotes: 4
Views: 2894
Reputation: 101
You may need to reset Header of Response in the interceptor too.
okhttp3.Response originalResponse = chain.proceed(request);
if (NetUtils.hasNetwork(mContext)) {
//when the network is available, you use the '@Headers' cache time in the interface
String cacheControl = request.cacheControl().toString();
int maxAge = 60; // read from cache for 1 minute
return originalResponse.newBuilder()
.header("Cache-Control", cacheControl)
//.header("Cache-Control", "public, max-age=" + maxAge)
.removeHeader("Progma")
.build();
} else {
return originalResponse.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=2419200") //60 * 60 * 24 * 28 4weeks
.removeHeader("Pragma")
.build();
}
Upvotes: 0
Reputation: 489
You'll have to use Builder
OkHttpClient client = new OkHttpClient.Builder()
.cache(cache).build();
Upvotes: 0
Reputation: 310
use as
File cacheDir = new File(MyApplication.getContext().getCacheDir(), cache);
myCache= new Cache(cacheDir, cacheSize);
okHttpClient = new OkHttpClient();
okHttpClient.setCache(myCache);
Upvotes: 1