Need to Make Your App Work Offline Without Extra Work? Retrofit2 to The Rescue

Julián Falcionelli
Major League

--

Assume the following scenario:

There is an application already developed and the customer requests that the application magically should work offline and that requirement was not contemplated from the initial stage of development.

If you are using Retrofit2, doing this will not take more than a few hours (15 minutes if you copy and paste the following code 😬).

Clarification, this is not a guide on how to use Retrofit2, for more information go to: http://square.github.io/retrofit/

Setting Offline Mode on a Retrofit instance

When we are setting our Retrofit instance, we need to setup the HTTP client which is our desired caching configuration and allows us to add different interceptors to check the network connection status each time a call is made and know if we need to obtain the data from cache or not.

Here's an example:

That's all! Now you just need to use this retrofit instance to create your networking classes (getRetrofit().create(NetworkInterface.class)) and your app will work without internet if networking calls were previously called and were cached.

Also remember that the HttpCache is a serializable file, not a database, therefore the performance may not be optimal for large data flows.

We can use different strategies to take advantage of the caching provided by retrofit, such as supposing that our customer asks that some information that doesn't change very often should load immediately (regardless if there is an internet connection or not) and only update the UI if the data has been changed.

One way to solve this is to have a Retrofit instance that always consults in the cache and then asks the server to update the information that is in the cache with the retrofit instance that we already created.

Here is a full example of both functionalities, offline mode, and Retrofit cached instance:

The key here is that the cache instance is shared by both Retrofit instances.

Also, there is a clean() method if you want to clear HTTP data in case the user logout.

So now you must have 2 network interface instances, one created with the getRetrofit() instance for those calls that must be done on the backend (and check in the cache if there is no internet connection) and the calls that you want to check on cache always should be done with the getCachedRetrofit() instance (and if you get an empty response/error with this instance that means that the request it's not cached yet, so you must make the backend call with the getRetrofit() instance).

Example of use:

And in your Activity:

Well, that was all, just a quick example of the use of Retrofit cache. You can play with the different parameters to configure your cache (expiration time, etc), and the use of different retrofit instances according to what you need.

I hope you have understood everything, if you have any questions/need advice don’t hesitate to write. Also, I invite you to check out my other Android posts!

--

--