use of okhttp3.logging.HttpLoggingInterceptor in project Awesome-WanAndroid by JsonChao.
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 = chain -> {
Request request = chain.request();
if (!CommonUtils.isNetworkConnected()) {
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
}
Response response = chain.proceed(request);
if (CommonUtils.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;
};
// 设置缓存
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);
// cookie认证
builder.cookieJar(new CookiesManager());
return builder.build();
}
use of okhttp3.logging.HttpLoggingInterceptor in project loc-framework by lord-of-code.
the class OkHttpLoggingAutoConfiguration method okHttp3LoggingInterceptor.
@Bean
@ConditionalOnMissingBean
public HttpLoggingInterceptor okHttp3LoggingInterceptor(OkHttpClientProperties loggingProperties) {
HttpLoggingInterceptor httpLoggingInterceptor = Optional.ofNullable(logger).map(l -> new HttpLoggingInterceptor(logger)).orElseGet(HttpLoggingInterceptor::new);
Optional.ofNullable(loggingProperties.getLevel()).ifPresent(httpLoggingInterceptor::setLevel);
return httpLoggingInterceptor;
}
use of okhttp3.logging.HttpLoggingInterceptor in project Api-Startup-Android by salyangoz.
the class ApiClientProvider method getInstance.
public static ApiClient getInstance() {
if (apiClient == null) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(loggingInterceptor);
httpClient.addInterceptor(new HeaderInterceptor());
}
OAuthResponse oAuthResponse = OAuthResponse.get();
if (oAuthResponse != null && authInterceptor == null) {
authInterceptor = new AuthenticationInterceptor(oAuthResponse.getAccessToken());
// Add Bearer Authentication if Authentication Token Exists
if (!httpClient.interceptors().contains(authInterceptor)) {
httpClient.authenticator(new TokenAuthenticator());
httpClient.interceptors().add(authInterceptor);
}
}
builder.client(httpClient.build()).build();
retrofit = builder.build();
retrofit.responseBodyConverter(APIError.class, new Annotation[0]);
return retrofit.create(ApiClient.class);
}
use of okhttp3.logging.HttpLoggingInterceptor in project Ency by xiarunhao123.
the class HttpModule method provideOkHttpClient.
@Provides
@Singleton
OkHttpClient provideOkHttpClient(OkHttpClient.Builder builder, final Context context) {
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 (!AppNetWorkUtil.isNetworkConnected(context)) {
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
}
Response response = chain.proceed(request);
if (AppNetWorkUtil.isNetworkConnected(context)) {
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;
}
};
// 设置缓存
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 okhttp3.logging.HttpLoggingInterceptor in project apm-agent-java by elastic.
the class ReporterFactory method getOkHttpClient.
@Nonnull
OkHttpClient getOkHttpClient(ReporterConfiguration reporterConfiguration) {
final OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(reporterConfiguration.getServerTimeout(), TimeUnit.SECONDS);
if (!reporterConfiguration.isVerifyServerCert()) {
disableCertificateValidation(builder);
}
if (logger.isTraceEnabled()) {
final HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
builder.addInterceptor(loggingInterceptor);
}
return builder.build();
}
Aggregations