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

How to dynamically configure OkHttpClient when using Retrofit?

我在我的 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"

我配置Retrofit如下:-

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
        }
    }
}

然后在我的服务实现中:-

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

logcat 输出是 "excessive" 因为我的一些网络调用响应非常大 Json 文档。

我想要的是我可以在细粒度级别配置我的网络调用日志记录,例如逐个调用。

我能看到如何实现这一点的唯一方法是为每个日志记录级别创建配对的 OkHttpClient & retrofit,例如BODYHEADERSBASICNONE

如果能够在调用的基础上设置日志记录值并且只使用单个 OkHttpClient & retrofit 实例,那就太好了。

有什么方法可以在调用级别上设置不同的日志记录级别?

您可以创建自己的记录器并委托给 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()))