android java retrofit offline cache

Solutions on MaxInterview for android java retrofit offline cache by the best coders in the world

showing results for - "android java retrofit offline cache"
Liam
10 Mar 2020
1//  i.g siyanda.zama 16.05.20
2
3in the manifest folder
4<application
5//dont forget to put the name
6// create a java file named MyApplication.java
7    android:name=".MyApplication"
8    android:allowBackup="true"
9    android:icon="@mipmap/launcher"
10    android:label="@string/app_name"
11    android:roundIcon="@mipmap/launcher_round"
12    android:supportsRtl="true"
13    android:theme="@style/AppTheme"
14    android:usesCleartextTraffic="true"
15    android:networkSecurityConfig="@xml/network_security_config">
16    
17in the MyApplication.java file
18public class MyApplication extends Application {
19
20    private static MyApplication instance;
21
22    @Override
23    public void onCreate() {
24        super.onCreate();
25
26        if(instance == null){
27            instance = this;
28        }
29    }
30
31    public static MyApplication getInstance(){
32        return instance;
33    }
34
35    public static boolean hasNetwork(){
36        return instance.isNetworkConnected();
37    }
38
39    private boolean isNetworkConnected(){
40        ConnectivityManager cm =
41                (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
42
43        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
44        return activeNetwork != null &&
45                activeNetwork.isConnectedOrConnecting();
46    }
47}
48
49// now finally we need to edit our okhttps
50// open your api client file
51OkHttpClient okHttpClient = new OkHttpClient.Builder()
52                .cache(cache())
53                .addNetworkInterceptor(networkInterceptor()) // only used when network is on
54                .addNetworkInterceptor(networkInterceptor()) // only used when network is on
55                .addInterceptor(offlineInterceptor())
56                .build();
57
58
59// cache method
60private static Cache cache(){
61        return new Cache(new File(MyApplication.getInstance().getCacheDir(),"someIdentifier"), cacheSize);
62    }
63
64//offline network interceptor
65private static Interceptor offlineInterceptor() {
66        return new Interceptor() {
67            @Override
68            public Response intercept(Chain chain) throws IOException {
69                Log.d(TAG, "offline interceptor: called.");
70                Request request = chain.request();
71
72                // prevent caching when network is on. For that we use the "networkInterceptor"
73                if (!MyApplication.hasNetwork()) {
74                    CacheControl cacheControl = new CacheControl.Builder()
75                            .maxStale(7, TimeUnit.DAYS)
76                            .build();
77
78                    request = request.newBuilder()
79                            .removeHeader(HEADER_PRAGMA)
80                            .removeHeader(HEADER_CACHE_CONTROL)
81                            .cacheControl(cacheControl)
82                            .build();
83                }
84
85                return chain.proceed(request);
86            }
87        };
88    }
89//online network interceptor
90 private static Interceptor networkInterceptor() {
91        return new Interceptor() {
92            @Override
93            public Response intercept(Chain chain) throws IOException {
94                Log.d(TAG, "network interceptor: called.");
95
96                Response response = chain.proceed(chain.request());
97
98                CacheControl cacheControl = new CacheControl.Builder()
99                        .maxAge(5, TimeUnit.SECONDS)
100                        .build();
101
102                return response.newBuilder()
103                        .removeHeader(HEADER_PRAGMA)
104                        .removeHeader(HEADER_CACHE_CONTROL)
105                        .header(HEADER_CACHE_CONTROL, cacheControl.toString())
106                        .build();
107            }
108        };
109    }
110
111