Search in sources :

Example 1 with BaseUrlInterceptor

use of com.cg.baseproject.request.retrofit.interceptor.BaseUrlInterceptor in project BaseProject by fly803.

the class RetrofitRequestManager method genericClient.

public static OkHttpClient genericClient() {
    /**
     * 类型为BASIC,其实日志级别分为4类:NONE、BASIC、HEADERS、BODY。
     *         大家看下我打印出来的日志,就知道这4类的区别了。
     *         1、NONE
     *         没有任何log
     *         2、BASIC
     *         请求/响应行
     *         basic的格式:
     *         --> POST 地址 http/1.1 (0-byte body)
     *         <-- 200 OK 地址 (154ms, unknown-length body)
     *         3、HEADERS
     *         请求/响应行 + 头
     *         4、BODY
     *         请求/响应行 + 头 + 体
     */
    final OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.connectTimeout(15, TimeUnit.SECONDS);
    builder.readTimeout(20, TimeUnit.SECONDS);
    builder.writeTimeout(20, TimeUnit.SECONDS);
    // 错误重连
    builder.retryOnConnectionFailure(true);
    // 新建log拦截器
    final HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {

        @Override
        public void log(String message) {
            Log.w(BaseProjectConfig.TAG, "genericClient log: " + message);
        }
    });
    HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BASIC;
    loggingInterceptor.setLevel(level);
    if (BaseProjectConfig.DEBUG) {
        // 添加retrofit日志打印 }
        builder.addInterceptor(loggingInterceptor);
        // builder.addInterceptor(new HeaderInterceptor());
        builder.addInterceptor(new BaseUrlInterceptor());
    }
    OkHttpClient httpClient = builder.build();
    return httpClient;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) BaseUrlInterceptor(com.cg.baseproject.request.retrofit.interceptor.BaseUrlInterceptor)

Aggregations

BaseUrlInterceptor (com.cg.baseproject.request.retrofit.interceptor.BaseUrlInterceptor)1 OkHttpClient (okhttp3.OkHttpClient)1 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)1