android retrofit offline cache code example
Example: android java retrofit offline cache
in the manifest folder
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config">
in the MyApplication.java file
public class MyApplication extends Application {
private static MyApplication instance;
@Override
public void onCreate() {
super.onCreate();
if(instance == null){
instance = this;
}
}
public static MyApplication getInstance(){
return instance;
}
public static boolean hasNetwork(){
return instance.isNetworkConnected();
}
private boolean isNetworkConnected(){
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
}
}
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cache(cache())
.addNetworkInterceptor(networkInterceptor())
.addNetworkInterceptor(networkInterceptor())
.addInterceptor(offlineInterceptor())
.build();
private static Cache cache(){
return new Cache(new File(MyApplication.getInstance().getCacheDir(),"someIdentifier"), cacheSize);
}
private static Interceptor offlineInterceptor() {
return new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Log.d(TAG, "offline interceptor: called.");
Request request = chain.request();
if (!MyApplication.hasNetwork()) {
CacheControl cacheControl = new CacheControl.Builder()
.maxStale(7, TimeUnit.DAYS)
.build();
request = request.newBuilder()
.removeHeader(HEADER_PRAGMA)
.removeHeader(HEADER_CACHE_CONTROL)
.cacheControl(cacheControl)
.build();
}
return chain.proceed(request);
}
};
}
private static Interceptor networkInterceptor() {
return new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Log.d(TAG, "network interceptor: called.");
Response response = chain.proceed(chain.request());
CacheControl cacheControl = new CacheControl.Builder()
.maxAge(5, TimeUnit.SECONDS)
.build();
return response.newBuilder()
.removeHeader(HEADER_PRAGMA)
.removeHeader(HEADER_CACHE_CONTROL)
.header(HEADER_CACHE_CONTROL, cacheControl.toString())
.build();
}
};
}