如何在 request.newBuilder() 中调用 httpLoggingInterceptor
How to call httpLoggingInterceptor inside request.newBuilder()
正如我研究发现的那样,.client(OkHttptvariableName)
在构建器中使用,以便将 api 响应打印到 logcat.
但就我而言,我无法使用 request.newBuilder.
调用 .client inbuild 方法
以下是我的代码片段。
请帮助我。
val httpLoggingInterceptor = HttpLoggingInterceptor()
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
val okHttpClient: OkHttpClient = OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build()
when {
UserInfo.loginStatus -> {
request = request.newBuilder()
.header("Authorization", UserInfo.token)
.build()
}
else -> {
request = request.newBuilder()
.build()
}
}
看这个例子:
val httpClient = OkHttpClient.Builder()
if (BuildConfig.DEBUG) {
val logging = HttpLoggingInterceptor()
logging.setLevel(HttpLoggingInterceptor.Level.BODY)
httpClient.addInterceptor(logging)
}
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build()
}
有关详细信息,请参阅:https://futurestud.io/tutorials/retrofit-2-enable-logging-for-development-builds-only
正如我研究发现的那样,.client(OkHttptvariableName)
在构建器中使用,以便将 api 响应打印到 logcat.
但就我而言,我无法使用 request.newBuilder.
调用 .client inbuild 方法
以下是我的代码片段。
请帮助我。
val httpLoggingInterceptor = HttpLoggingInterceptor()
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
val okHttpClient: OkHttpClient = OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build()
when {
UserInfo.loginStatus -> {
request = request.newBuilder()
.header("Authorization", UserInfo.token)
.build()
}
else -> {
request = request.newBuilder()
.build()
}
}
看这个例子:
val httpClient = OkHttpClient.Builder()
if (BuildConfig.DEBUG) {
val logging = HttpLoggingInterceptor()
logging.setLevel(HttpLoggingInterceptor.Level.BODY)
httpClient.addInterceptor(logging)
}
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build()
}
有关详细信息,请参阅:https://futurestud.io/tutorials/retrofit-2-enable-logging-for-development-builds-only