I'm really stuck at this problem.
I want to cache server response for some time to use the data when device is offline.
So I set a cache to my OkHttpClient
.
Also I set Cache-Contlol
header.
Here is my code:
public class MainActivity extends AppCompatActivity {
Retrofit retrofit;
RecyclerView recyclerView;
RandomUserAdapter mAdapter;
Cache cache;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
cache = provideOkHttpCache();
Timber.plant(new Timber.DebugTree());
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
populateUsers();
}
});
Interceptor cacheInterceptor = new Interceptor() {
@Override
public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
okhttp3.Response originalResponse = chain.proceed(chain.request());
okhttp3.Response modifyedResponse = originalResponse;
if (true) {
modifyedResponse = originalResponse.newBuilder()
.header("Cache-Control", "max-age=86400")
.build();
}
return modifyedResponse;
}
};
HttpLoggingInterceptor httpLoggingInterceptor = new
HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(@NonNull String message) {
Timber.i(message);
}
});
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient()
.newBuilder()
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(cacheInterceptor)
.cache(cache)
.build();
retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl("https://randomuser.me/")
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
populateUsers();
}
private void initViews() {
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
button = findViewById(R.id.button);
}
private void populateUsers() {
Call<RandomUsers> randomUsersCall = getRandomUserService().getRandomUsers(10);
randomUsersCall.enqueue(new Callback<RandomUsers>() {
@Override
public void onResponse(Call<RandomUsers> call, @NonNull Response<RandomUsers> response) {
if (response.isSuccessful()) {
Log.v("cache-control", "using cache = " + isFromCache(response));
mAdapter = new RandomUserAdapter();
mAdapter.setItems(response.body().getResults());
recyclerView.setAdapter(mAdapter);
}
}
@Override
public void onFailure(Call<RandomUsers> call, Throwable t) {
Timber.i(t.getMessage());
Log.v("cache-control", "response failure");
}
});
}
public RandomUsersApi getRandomUserService() {
return retrofit.create(RandomUsersApi.class);
}
Cache provideOkHttpCache() {
int cacheSize = 10 * 1024 * 1024; // 100 MiB
File cacheDir = new File("cachedirectory");
Cache cache = new Cache(cacheDir, cacheSize);
return cache;
}
public boolean isFromCache(Response response) {
return response.raw().cacheResponse() != null;
}
}
Looks like everything is going to be ok. Then I do this:
- run app
- look at the OkHttp logs and on the screen to become sure that we have recieved data successfully
- close app
- turn on flight-mode on device (all networks off)
- run app
At this moment I expect that app is going to use cached data. But it doesn't.
I have only blank screen and HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "randomuser.me": No address associated with hostname
in my logcat.