android java retrofit offline cache
Java
// i.g siyanda.zama 16.05.20
in the manifest folder
<application
//dont forget to put the name
// create a java file named MyApplication.java
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();
}
}
// now finally we need to edit our okhttps
// open your api client file
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cache(cache())
.addNetworkInterceptor(networkInterceptor()) // only used when network is on
.addNetworkInterceptor(networkInterceptor()) // only used when network is on
.addInterceptor(offlineInterceptor())
.build();
// cache method
private static Cache cache(){
return new Cache(new File(MyApplication.getInstance().getCacheDir(),"someIdentifier"), cacheSize);
}
//offline network interceptor
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();
// prevent caching when network is on. For that we use the "networkInterceptor"
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);
}
};
}
//online network interceptor
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();
}
};
}
Also in Java:
- Title
- int java
- Category
- Java
- Title
- java random number in range
- Category
- Java
- Title
- how to return the first character in an array from a method java
- Category
- Java
- Title
- java implement interface
- Category
- Java
- Title
- java fileinputstream
- Category
- Java
- Title
- boolean java.lang.String.equals(java.lang.Object)' on a null object reference
- Category
- Java
- Title
- math min max java
- Category
- Java
- Title
- java class array of objects
- Category
- Java
- Title
- while loops java
- Category
- Java
- Title
- how to stop screen rotation in android code
- Category
- Java
- Title
- final variables in java
- Category
- Java
- Title
- how to remove a certain string in a arraylist java
- Category
- Java
- Title
- how to make a pre set list java
- Category
- Java
- Title
- Java loop throug gson JsonElement
- Category
- Java
- Title
- how to install java 8 in ubuntu 16.04
- Category
- Java
- Title
- java while loop example
- Category
- Java
- Title
- java substring
- Category
- Java
- Title
- split method in java
- Category
- Java
- Title
- how to create dynamic string array in java
- Category
- Java
- Title
- strong password regular expression java
- Category
- Java
- Title
- JavaFX onMouseEntered
- Category
- Java
- Title
- download spring
- Category
- Java
- Title
- reverse shuffle merge
- Category
- Java
- Title
- remove last character from string java
- Category
- Java
- Title
- how to get array input in java
- Category
- Java
- Title
- how to fix Index 2 out of bounds for length 2 when looping through an array in java
- Category
- Java
- Title
- how to write a java for loop?
- Category
- Java
- Title
- abstract class java constructor
- Category
- Java
- Title
- IN APPLet how to disable a button in java
- Category
- Java
- Title
- else statement java
- Category
- Java
- Title
- how to create a random number in java
- Category
- Java
- Title
- error attribute fabattached not found
- Category
- Java
- Title
- string to int java
- Category
- Java
- Title
- inputstream to string java
- Category
- Java
- Title
- java string split underscore
- Category
- Java
- Title
- java while
- Category
- Java
- Title
- java quit application
- Category
- Java
- Title
- create object of static class in java
- Category
- Java
- Title
- multiplication program java
- Category
- Java
- Title
- list java oracle
- Category
- Java
- Title
- how to take input in java
- Category
- Java
- Title
- creating the functional interface in java
- Category
- Java
- Title
- link to method javadoc
- Category
- Java
- Title
- palindrome function java
- Category
- Java
- Title
- java list of a class has a string that is equal to
- Category
- Java
- Title
- java type casting
- Category
- Java
- Title
- java replace element in list
- Category
- Java
- Title
- how to make a list java
- Category
- Java
- Title
- java replaceall single character
- Category
- Java
- Title
- count occurrences of character in string java 8
- Category
- Java
- Title
- android dynamically create layer-list with item and shape site:stackoverflow.com
- Category
- Java
- Title
- osmdroid offline map does not show
- Category
- Java
- Title
- iterate over map keys java
- Category
- Java
- Title
- java script find screen size of device
- Category
- Java
- Title
- spring code in java
- Category
- Java
- Title
- kotlin enable and disable parents view children
- Category
- Java
- Title
- how to declare an array in java
- Category
- Java
- Title
- java script to detect the crome browser
- Category
- Java
- Title
- how to make a dictionary in java
- Category
- Java
- Title
- onbackpressed android
- Category
- Java
- Title
- hibernate Unknown integral data type for ids : java.lang.String
- Category
- Java
- Title
- how to get token oauth2.0 java example response
- Category
- Java
- Title
- Java.awt graphics tutorial
- Category
- Java
- Title
- java packages example
- Category
- Java
- Title
- convert java list to array
- Category
- Java
- Title
- shorthand if java without else
- Category
- Java
- Title
- java stack peek
- Category
- Java
- Title
- how to create an abstract method in java
- Category
- Java
- Title
- Removing DOM nodes when traversing a NodeList
- Category
- Java
- Title
- stack overflow recyclerview
- Category
- Java
- Title
- gradle require java version
- Category
- Java
- Title
- java rest client response json
- Category
- Java
- Title
- java execute for cycle parallel thread
- Category
- Java
- Title
- guess the number java
- Category
- Java
- Title
- queue implementation in java using arraylist
- Category
- Java
- Title
- recursive function for fibonacci series in java javascript
- Category
- Java
- Title
- 2d arrays | java
- Category
- Java
- Title
- dataframe to dict without index
- Category
- Java
- Title
- arraylist set method
- Category
- Java
- Title
- how to return the lower of two values in one line java
- Category
- Java
- Title
- arraylist with values
- Category
- Java
- Title
- cannot fit requested classes in a single dex file
- Category
- Java
- Title
- java output length of each line in a text document
- Category
- Java
- Title
- html top padding
- Category
- Java
- Title
- not equal to java
- Category
- Java
- Title
- java script snippet for responsive
- Category
- Java
- Title
- java android development get element by id
- Category
- Java
- Title
- java get size of array
- Category
- Java
- Title
- write file from a specific location in java
- Category
- Java
- Title
- java read lines from file
- Category
- Java
- Title
- how intent
- Category
- Java
- Title
- Compare integers java sort
- Category
- Java
- Title
- simple calculator program in java
- Category
- Java
- Title
- java iterate through hashmap
- Category
- Java
- Title
- loop array using stream java
- Category
- Java
- Title
- how to get the dimensions of a 2d array in java
- Category
- Java
- Title
- how to declare a linked list in java
- Category
- Java
- Title
- java call another constructor
- Category
- Java
- Title
- java return new instance of generic type
- Category
- Java
- Title
- java override equals and hashcode
- Category
- Java