Search in sources :

Example 1 with HttpLoggingInterceptor

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();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) HttpLoggingInterceptor(com.connxun.ltcx.components.okhttp.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 2 with HttpLoggingInterceptor

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);
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) HttpLoggingInterceptor(com.connxun.ltcx.components.okhttp.HttpLoggingInterceptor)

Aggregations

HttpLoggingInterceptor (com.connxun.ltcx.components.okhttp.HttpLoggingInterceptor)2 StethoInterceptor (com.facebook.stetho.okhttp3.StethoInterceptor)2 OkHttpClient (okhttp3.OkHttpClient)2 Provides (dagger.Provides)1 Singleton (javax.inject.Singleton)1 Request (okhttp3.Request)1 Retrofit (retrofit2.Retrofit)1