我正在创建一个android应用,并且正在使用改造来使用api网络服务。我的问题是,当我使用注释添加标题时,它可以工作。
@Headers("Authorization: jwt-token")
@POST("appusers-service/signin/identification")
suspend fun authentication(@Body body : LoginIdentificationDto.Params) : NetworkResponse<LoginIdentificationDto.Result, LoginIdentificationDto.Errors>
但是当我使用拦截器动态添加标头时,它不起作用
@Provides
@Singleton
fun provideHttpClient() : OkHttpClient
{
return OkHttpClient.Builder()
.callTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.addNetworkInterceptor {
val builder : Request.Builder = it.request().newBuilder()
builder.addHeader(AUTHORIZATION_HEADER, "jwt-token")
return@addNetworkInterceptor it.proceed(builder.build())
}
.build()
}
@Provides
@Singleton
fun provideRetrofit(httpClient : OkHttpClient, mapper : ObjectMapper) : Retrofit
{
return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(httpClient)
.addCallAdapterFactory(NetworkResponseAdapterFactory())
.addConverterFactory(JacksonConverterFactory.create(mapper))
.build()
}
请问如何解决这个问题?
答案 0 :(得分:0)
尝试这个
docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:3 python dwhproxy.py writeweather "Hannover,de"
docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:3 python dwhproxy.py writetwitter "MarcTV"
docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:3 python dwhproxy.py writeyoutube
请注意,我已将 addNetworkInterceptor 更改为 addInterceptor 。 有关更多信息,您可以参考此答案 https://stackoverflow.com/a/37120830/9394700
答案 1 :(得分:0)
我正在做的事情是开设一堂AuthInterceptor
class AuthInterceptor : Interceptor {
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val authenticatedRequest = request.newBuilder()
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", UserData.getAuthToken())
.build()
return chain.proceed(authenticatedRequest)
}
}
然后在您的OkHtttpClient
上像这样使用
val ohHttpClient = OkHttpClient.Builder()
ohHttpClient.addInterceptor(AuthInterceptor())