Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用kotlin在android中添加http日志拦截器和另一个拦截器?_Android_Kotlin_Retrofit_Retrofit2_Okhttp - Fatal编程技术网

如何使用kotlin在android中添加http日志拦截器和另一个拦截器?

如何使用kotlin在android中添加http日志拦截器和另一个拦截器?,android,kotlin,retrofit,retrofit2,okhttp,Android,Kotlin,Retrofit,Retrofit2,Okhttp,我正在努力为我的改装客户制作两个不同的拦截器 我已经有一个拦截器将我的查询api_密钥添加到请求中,我正在尝试添加 HttpLoggingInterceptor 对于相同的改装实例,有没有办法做到这一点 这是我的密码 import com.example.tvapptest.Services.MovieService import com.example.tvapptest.Services.ShowService import com.example.tvapptest.Utils.Const

我正在努力为我的改装客户制作两个不同的拦截器 我已经有一个拦截器将我的查询api_密钥添加到请求中,我正在尝试添加

HttpLoggingInterceptor

对于相同的改装实例,有没有办法做到这一点

这是我的密码

import com.example.tvapptest.Services.MovieService
import com.example.tvapptest.Services.ShowService
import com.example.tvapptest.Utils.Constants
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitConfig {

private val interceptor : HttpLoggingInterceptor = HttpLoggingInterceptor().apply {
    this.level = HttpLoggingInterceptor.Level.BODY
}

private val client : OkHttpClient = OkHttpClient.Builder().apply {
    this.addInterceptor(interceptor)
}.build()


private val clientapi : OkHttpClient = OkHttpClient.Builder().apply {
    this.addNetworkInterceptor(ApiInterceptor())
}.build()

// use lazy to insure that only one instance of retrofit will be used - no duplication
private val retrofit : Retrofit by lazy {
    Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(Constants.BASE_URL)
         // the issue is here how can i add another interceptor to the same client here
        //.client(client)
        .client(clientapi)
        .build()
}


val movieService : MovieService by lazy {
    retrofit.create(MovieService::class.java)
}

val showService : ShowService by lazy {
    retrofit.create(ShowService::class.java)
}
}
这是我的ApiInterceptor类

package com.example.tvapptest.Network

import com.example.tvapptest.Utils.Constants
import okhttp3.Interceptor
import okhttp3.Response

// this class is used to intercept the request and add the query param api_key
class ApiInterceptor() : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
    val original = chain.request()
    val originalHttpUrl  = original.url
    val requestBuilder = original.newBuilder().url(originalHttpUrl.newBuilder().addQueryParameter("api_key",Constants.API_KEY).build())
    return  chain.proceed(requestBuilder.build())
}
}

想要两个不同的客户有什么理由吗? 看起来您可以只使用一个拦截器,然后将两个拦截器添加到同一个客户端

这与科特林的情况类似

OkHttpClient.Builder()
    .addInterceptor(interceptor)
    .addNetworkInterceptor(ApiInterceptor())
    .build()
}

你说得对,非常感谢