use of com.connxun.ltcx.components.okhttp.HttpLoggingInterceptor in project ttdj by soonphe.
the class ApplicationModule method provideOkHttpClient.
@Provides
@Singleton
OkHttpClient provideOkHttpClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, TimeUnit.SECONDS).retryOnConnectionFailure(true);
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addNetworkInterceptor(new StethoInterceptor());
builder.addInterceptor(logging);
// 请求header拦截器——存在userId/token即添加
builder.addInterceptor(chain -> {
Request originalRequest = chain.request();
if (StringUtils.isEmpty(CacheUtils.getInstance().getString(Constants.USER_ID) + "") || StringUtils.isEmpty(CacheUtils.getInstance().getString(Constants.USER_TOKEN))) {
return chain.proceed(originalRequest);
}
Request authorised = originalRequest.newBuilder().addHeader("userId", CacheUtils.getInstance().getString(Constants.USER_ID).toString()).addHeader("token", CacheUtils.getInstance().getString(Constants.USER_TOKEN) + "").build();
return chain.proceed(authorised);
});
return builder.build();
}
use of com.connxun.ltcx.components.okhttp.HttpLoggingInterceptor in project ttdj by soonphe.
the class AppApi method getDynamicAppApi.
/**
* 动态请求路径+默认json解析
*
* @param baseUrl
*/
public AppApiService getDynamicAppApi(String baseUrl) {
OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, TimeUnit.SECONDS).retryOnConnectionFailure(true);
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addNetworkInterceptor(new StethoInterceptor());
builder.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder().client(builder.build()).baseUrl(baseUrl).addCallAdapterFactory(RxJava2CallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create()).build();
return retrofit.create(AppApiService.class);
}
Aggregations