gpt4 book ai didi

android - 最新 Okhttp 中的主机拦截器 HttpUrl.parse IllegalArguementException

转载 作者:行者123 更新时间:2023-12-02 13:16:38 24 4
gpt4 key购买 nike

I have to intercept host at run time . As my url is dynamic. below code is working fine in old okhttp3


使用旧的 Okhttp
class HostSelectionInterceptor @Inject constructor(val chiPrefs: ChiPrefs): Interceptor{

override fun intercept(chain: Interceptor.Chain): Response {
var request: Request = chain.request()

var host = String.format(Locale.ENGLISH, "https://%s.cognitiveintl.com",
chiPrefs.sitePrefix())

request.url().pathSegments().forEach {
host += "/$it"
}

if(host.isNotEmpty()){
val newUrl = HttpUrl.parse(host)
request = request.newBuilder().url(newUrl!!).build()
}
return chain.proceed(request)
}
}
但升级到最新版本后。
val newUrl = HttpUrl.parse(host) // deprecated..
HttpUrl.parse .被弃用..
在研发之后,我更新了我的代码
val newUrl = request.url.newBuilder()
.host(host) ///crashed at this line
.build()
request = request.newBuilder()
.url(newUrl)
.build()
它给出 IllegalArguementException 。提出解决方案。
碰撞 :
FATAL EXCEPTION: OkHttp Dispatcher
Process: com.chi.doctorapp.dev, PID: 2906
java.lang.IllegalArgumentException: unexpected host: https://chi-dev1.cognitiveintl.com/api/doctor_app/GetProfile
at okhttp3.HttpUrl$Builder.host(HttpUrl.kt:961)
at com.chi.doctorapp.di.interceptors.HostSelectionInterceptor.intercept(HostSelectionInterceptor.kt:28)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)

最佳答案

替换这个:

HttpUrl.parse(host)
有了这个:
host.toHttpUrlOrNull()
您将需要此导入:
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull()
这记录在 upgrade guide .

关于android - 最新 Okhttp 中的主机拦截器 HttpUrl.parse IllegalArguementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63110405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com