Skip to content

Instantly share code, notes, and snippets.

@malikkurosaki
Last active August 2, 2019 02:35
Show Gist options
  • Save malikkurosaki/137a451bd47395f190a1d129604ca04c to your computer and use it in GitHub Desktop.
Save malikkurosaki/137a451bd47395f190a1d129604ca04c to your computer and use it in GitHub Desktop.
retrofit offline

Retrofit Mode Offline

deklarasi

private OkHttpClient okHttpClient;
    // PENGATURAN RETROFIT MODE OFFLINE
    boolean isConnected = false;
    public static final String HEADER_CACHE_CONTROL = "Cache-Control";
    public static final String HEADER_PRAGMA = "Pragma";

Divinisi di Activity

 okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(15, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(15, TimeUnit.SECONDS)
                .addInterceptor(provideOfflineCacheInterceptor())
                .addNetworkInterceptor(provideCacheInterceptor())
                .cache(provideCache())
                .build();

inceptor method

private Interceptor provideCacheInterceptor() {
        return chain -> {
            okhttp3.Response response = chain.proceed(chain.request());

            CacheControl cacheControl;

            if (isConnected) {
                cacheControl = new CacheControl.Builder()
                        .maxAge(0, TimeUnit.SECONDS)
                        .build();
            } else {
                cacheControl = new CacheControl.Builder()
                        .maxStale(7, TimeUnit.DAYS)
                        .build();
            }

            return response.newBuilder()
                    .removeHeader(HEADER_PRAGMA)
                    .removeHeader(HEADER_CACHE_CONTROL)
                    .header(HEADER_CACHE_CONTROL, cacheControl.toString())
                    .build();

        };
    }

inceptor offline method_

private Interceptor provideOfflineCacheInterceptor() {
        return chain -> {
            Request request = chain.request();

            if (!isConnected) {
                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);
        };
    }

chache

private Cache provideCache() {
       Cache cache = null;
       try {
           cache = new Cache(new File(context.getCacheDir(), "http-cache"),
                   10 * 1024 * 1024); // 10 MB
       } catch (Exception e) {
           Log.e(TAG, "Could not create Cache!");
       }
       return cache;
   }

Penerapan Pada Retrofit

Retrofit retroDistribution = new Retrofit.Builder()
               .addConverterFactory(GsonConverterFactory.create())
               .baseUrl(URLNYA)
               .client(okHttpClient)
               .build();
       PemanggilApi pemanggilApiDistribution = retroDistribution.create(PemanggilApi.class);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment