Last active
February 4, 2018 17:36
-
-
Save mohsenoid/a70507c1bdb5f9cef4bf2aa119878614 to your computer and use it in GitHub Desktop.
Sample ClientModule OkHttpClient https://hackernoon.com/yet-another-mvp-article-part-3-calling-apis-using-retrofit-23757f4eee05
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mirhoseini.stylight.domain; | |
/*...*/ | |
@Module | |
public class ClientModule { | |
/*...*/ | |
@Singleton | |
@Provides | |
public OkHttpClient provideOkHttpClient(HttpLoggingInterceptor loggingInterceptor, | |
@Named("networkTimeoutInSeconds") int networkTimeoutInSeconds, | |
@Named("isDebug") boolean isDebug, | |
Cache cache, | |
@Named("cacheInterceptor") Interceptor cacheInterceptor, | |
@Named("offlineInterceptor") Interceptor offlineCacheInterceptor, | |
@Named("retryInterceptor") Interceptor retryInterceptor) { | |
OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder() | |
.addNetworkInterceptor(cacheInterceptor) | |
.addInterceptor(offlineCacheInterceptor) | |
.addInterceptor(retryInterceptor) | |
.cache(cache) | |
.connectTimeout(networkTimeoutInSeconds, TimeUnit.SECONDS); | |
//show logs if app is in Debug mode | |
if (isDebug) | |
okHttpClient.addInterceptor(loggingInterceptor); | |
return okHttpClient.build(); | |
} | |
/*...*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment