use of dagger.Provides in project kickmaterial by byoutline.
the class GlobalModule method providesGson.
@Provides
Gson providesGson() {
GsonBuilder builder = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
JsonDeserializer<DateTime> deserializer = (json, typeOfT, context) -> new DateTime(json.getAsJsonPrimitive().getAsLong() * 1000);
builder.registerTypeAdapter(DateTime.class, deserializer);
return builder.create();
}
use of dagger.Provides in project philm by chrisbanes.
the class NetworkProvider method provideTraktClient.
@Provides
@Singleton
public Trakt provideTraktClient(@CacheDirectory File cacheLocation) {
Trakt trakt = new PhilmTrakt(cacheLocation);
trakt.setApiKey(Constants.TRAKT_API_KEY);
trakt.setIsDebug(Constants.DEBUG_NETWORK);
return trakt;
}
use of dagger.Provides in project GeekNews by codeestX.
the class HttpModule method provideClient.
@Singleton
@Provides
OkHttpClient provideClient(OkHttpClient.Builder builder) {
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
builder.addInterceptor(loggingInterceptor);
}
File cacheFile = new File(Constants.PATH_CACHE);
Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
Interceptor cacheInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!SystemUtil.isNetworkConnected()) {
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
}
Response response = chain.proceed(request);
if (SystemUtil.isNetworkConnected()) {
int maxAge = 0;
// 有网络时, 不缓存, 最大保存时长为0
response.newBuilder().header("Cache-Control", "public, max-age=" + maxAge).removeHeader("Pragma").build();
} else {
// 无网络时,设置超时为4周
int maxStale = 60 * 60 * 24 * 28;
response.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale).removeHeader("Pragma").build();
}
return response;
}
};
// Interceptor apikey = new Interceptor() {
// @Override
// public Response intercept(Chain chain) throws IOException {
// Request request = chain.request();
// request = request.newBuilder()
// .addHeader("apikey",Constants.KEY_API)
// .build();
// return chain.proceed(request);
// }
// }
// 设置统一的请求头部参数
// builder.addInterceptor(apikey);
//设置缓存
builder.addNetworkInterceptor(cacheInterceptor);
builder.addInterceptor(cacheInterceptor);
builder.cache(cache);
//设置超时
builder.connectTimeout(10, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
builder.writeTimeout(20, TimeUnit.SECONDS);
//错误重连
builder.retryOnConnectionFailure(true);
return builder.build();
}
use of dagger.Provides in project MovieGuide by esoxjem.
the class NetworkModule method provideOkHttpClient.
@Provides
@Singleton
OkHttpClient provideOkHttpClient() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return new okhttp3.OkHttpClient.Builder().connectTimeout(CONNECT_TIMEOUT_IN_MS, TimeUnit.MILLISECONDS).addInterceptor(loggingInterceptor).build();
}
use of dagger.Provides in project SeriesGuide by UweTrottmann.
the class HttpClientModule method provideOkHttpClient.
/**
* Returns this apps {@link OkHttpClient} with enabled response cache. Should be used with API
* calls.
*/
@Provides
@Singleton
OkHttpClient provideOkHttpClient(SgApp app, Cache cache) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.connectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
builder.readTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
builder.addInterceptor(new SgTmdbInterceptor());
builder.addNetworkInterceptor(new SgTheTvdbInterceptor(app));
builder.addNetworkInterceptor(new SgTraktInterceptor(app));
builder.authenticator(new AllApisAuthenticator(app));
builder.cache(cache);
return builder.build();
}
Aggregations