Created
January 21, 2017 07:45
-
-
Save agustarc/fb7b5b83b7a42327ee5ede62fe8e8ef6 to your computer and use it in GitHub Desktop.
This file contains 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
@Module | |
public class SampleModule { | |
private static final int CONNECT_TIMEOUT = 15; | |
private static final int WRITE_TIMEOUT = 15; | |
private static final int READ_TIMEOUT = 15; | |
private static final String baseUrl; // your base url; | |
@Provides | |
@Singleton | |
Cache provideOkHttpCache(Application application) { | |
final int cacheSize = 10 * 1024 * 1024; // 10MB | |
return new Cache(application.getCacheDir(), cacheSize); | |
} | |
@Provides | |
@Singleton | |
Gson provideGson() { | |
return new GsonBuilder() | |
.registerTypeAdapterFactory(AutoValueGsonFactory.create()) | |
.create(); | |
} | |
@Provides | |
@Singleton | |
OkHttpClient provideOkHttpClient(Cache cache) { | |
return new OkHttpClient.Builder() | |
.cache(cache) | |
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS) | |
.writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS) | |
.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS) | |
.build(); | |
} | |
@Provides | |
@Singleton | |
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) { | |
return new Retrofit.Builder() | |
.addConverterFactory(GsonConverterFactory.create(gson)) | |
.baseUrl(baseUrl) | |
.client(okHttpClient) | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment