Caching With Retrofit — Store Responses Offline

Modifying your Retrofit to make your app offline-friendly without a database

Bapusaheb Patil
Sep 7, 2018 · 3 min read
Image for post
Image for post
Banner by Bapusaheb Patil • Logo from Square

Benefits of Caching

How you’ve been using Retrofit so far…

Creating a cache-friendly OkHttpClient

Step 1: Define a method to check for internet connectivity

Step 2: Defining the size of the cache

val cacheSize = (5 x 1024 x 1024).toLong()

Step 3: Creating the Cache variable

val myCache = Cache(context.cacheDir, cacheSize)

Step 4: Creating the OkHttpClient instance with an Interceptor

Putting it all together…


Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store