In this article, we will see how to cache the HTTP response and use the cached response when the user is offline using Retrofit and OkHttp.
OkHttp provides built-in support for caching HTTP responses. To enable caching in Retrofit, we need to configure an OkHttp cache. Following is how we can set it up.
Setup dependencies:
Include the following dependencies in the build.gradle file.
implementation 'com.squareup.retrofit2:retrofit:<version>'
implementation 'com.squareup.okhttp3:okhttp:<version>'
In the above, replace the version with the appropriate version. You can visit the following reference links and find the latest version of these dependencies.
Create offline OkHttp client with cache:
Now, create an instance of the OkHttpClient and configure it to use a cache. Define the cache size and the directory to store the cache.
File cacheDirectory = new File(context.getCacheDir(), "http-cache");
int cacheSize = 10 * 1024 * 1024; // 10 MB
Cache cache = new Cache(cacheDirectory, cacheSize);
OkHttpClient client = new OkHttpClient.Builder()
.cache(cache)
.build();
In the above example, we created a cache directory in the application’s cache directory with a cache size of 10 MB and created an OkHttpClient
instance using the OkHttpClient.Builder
and configure it to use the cache.
Configuring Retrofit to Use OkHttp Cache:
We have now the OkHttpClient
instance with caching enabled, we need to configure Retrofit to use this client.
With the OkHttpClient
instance, the Retrofit can leverage OkHttp’s caching capabilities.
Create retrofit instance
See the below example on how to create a retrofit instance.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.example.com/")
.client(client) // Use the OkHttp client with cache
.addConverterFactory(GsonConverterFactory.create())
.build();
In the above example, we created a Retrofit.Builder
instance and set the base URL of the API and also set the OkHttpClient
instance with cache as the client for Retrofit.
We also configured the desired converter factory, such as Gson, to parse the response.
Define API interface
Using retrofit Annotations, define the API interface as shown below.
public interface ApiService {
@GET("data")
Call<DataResponse> getData();
}
Make API request
Make the API requests using the defined API interface and Retrofit will automatically handle the caching based on the cache control headers returned by the server.
ApiService apiService = retrofit.create(ApiService.class);
Call<DataResponse> call = apiService.getData();
call.enqueue(new Callback<DataResponse>() {
@Override
public void onResponse(Call<DataResponse> call, Response<DataResponse> response) {
// Handle the response
}
@Override
public void onFailure(Call<DataResponse> call, Throwable t) {
// Handle the error
}
});
When the device is online, Retrofit will make a network request and cache the response.
When the device is offline and the cache is still valid based on the cache control headers, Retrofit will use the cached response instead of making a network request.