使用Retrofit时如何动态配置OkHttpClient?

时间:2019-06-26 08:28:12

标签: android logging retrofit2 okhttp3

我在Android应用程序中使用了Retrofit进行联网

api "com.squareup.retrofit2:retrofit:$retrofit2Version"
api "com.squareup.retrofit2:adapter-rxjava2:$retrofit2Version"
api "com.squareup.retrofit2:converter-scalars:$retrofit2Version"
api "com.squareup.retrofit2:converter-jackson:$retrofit2Version"

在开发项目时,使用

记录HTTP请求和响应非常有用。
api "com.squareup.okhttp3:logging-interceptor:$okhttp3Version"

我按如下方式配置改造:-

abstract class BaseService {

    internal val okHttpClient = OkHttpClient.Builder()
            .connectTimeout(OK_HTTP_CLIENT_TIMEOUT, TimeUnit.SECONDS)
            .readTimeout(OK_HTTP_CLIENT_TIMEOUT, TimeUnit.SECONDS)
            .writeTimeout(OK_HTTP_CLIENT_TIMEOUT, TimeUnit.SECONDS)
            .callTimeout(OK_HTTP_CLIENT_TIMEOUT, TimeUnit.SECONDS)
            .followSslRedirects(true)
            .retryOnConnectionFailure(true)
            .followRedirects(true)
            .addInterceptor(HTTP_HEADERS)
            .addInterceptor(HTTP_LOGGING_INTERCEPTOR)
            .build()

    companion object {

        private const val OK_HTTP_CLIENT_TIMEOUT: Long = 60

        private val HTTP_LOGGING_INTERCEPTOR = HttpLoggingInterceptor()
        private val HTTP_HEADERS = constructHeaderInterceptor()

        private const val HTTP_ORIGIN_HEADER = "Origin"
        private const val HTTP_ORIGIN_VALUE = "Android"

        private fun constructHeaderInterceptor(): Interceptor {
            return Interceptor {
                val request = it.request()
                val newRequest = request.newBuilder().addHeader(HTTP_ORIGIN_HEADER, HTTP_ORIGIN_VALUE).build()
                it.proceed(newRequest)
            }
        }

        init {
            HTTP_LOGGING_INTERCEPTOR.level = HttpLoggingInterceptor.Level.NONE
        }
    }
}

然后在我的Service实现中:-

class Service private constructor() : BaseService(), Api {

    private val service: Api

    init {
        val retrofit = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(JacksonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build()

        service = retrofit.create(Api::class.java)
   }


    override fun query(authenticationToken: String, request: Request): Single<Response<QueryResponse>> {
        return service.query(authenticationToken, request)
    }

    companion object {

        private val BASE_URL = BuildConfig.SDK_BASE_URL

        private val INSTANCE = Service()

        fun instance(): Api {
            return INSTANCE
        }
    }
}

我面临的问题是,当我使用

HttpLoggingInterceptor.Level.BODY

由于我的一些网络调用响应是非常大的Json文档,因此logcat的输出“过多”。

我想要的是我可以细粒度地配置网络呼叫记录,例如

尽管如此,我能看到如何实现此目标的唯一方法是为每个日志记录级别创建一个成对的OkHttpClientretrofit,例如BODYHEADERSBASICNONE

最好的做法是能够逐个呼叫地设置日志记录值,并且仅使用单个OkHttpClientretrofit实例。

有什么方法可以在每个呼叫级别上设置不同的日志记录级别吗?

1 个答案:

答案 0 :(得分:2)

您可以创建自己的记录器并将其委托给HttpLoggingInterceptor。

class MyLoggingInterceptor(private val logger: HttpLoggingInterceptor) : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        if (chain.request().url().toString().contains("condition")) {
            logger.level = HttpLoggingInterceptor.Level.BODY
        } else {
            logger.level = HttpLoggingInterceptor.Level.HEADERS
        }
        return logger.intercept(chain)
    }
}

然后将其添加到OkHttp构建器中

.addInterceptor(MyLoggingInterceptor(HttpLoggingInterceptor()))