Skip to content

Instantly share code, notes, and snippets.

@agustarc
Created January 21, 2017 07:45
Show Gist options
  • Save agustarc/fb7b5b83b7a42327ee5ede62fe8e8ef6 to your computer and use it in GitHub Desktop.
Save agustarc/fb7b5b83b7a42327ee5ede62fe8e8ef6 to your computer and use it in GitHub Desktop.
@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