该问题是由于HTTPS安全认证验证失败引起的。可能的解决方案是安装合适的证书。具体来说,可以通过自定义SSL Socket Factory并使用它来处理证书。示例代码如下:
val trustAllCerts = arrayOf(object : X509TrustManager {
override fun checkClientTrusted(chain: Array, authType: String) {
}
override fun checkServerTrusted(chain: Array, authType: String) {
try {
chain[0].checkValidity()
} catch (e: Exception) {
throw CertificateException("Certificate not valid or trusted.")
}
}
override fun getAcceptedIssuers(): Array = arrayOf()
})
val sslContext = SSLContext.getInstance("TLS")
sslContext.init(null, trustAllCerts, SecureRandom())
val sslSocketFactory = sslContext.socketFactory
val client = OkHttpClient.Builder().apply {
sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
hostnameVerifier { _, _ -> true }
}.build()
val retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
上一篇:AndroidKotlinregisterForActivityResult没有回调
下一篇:AndroidkotlinretrofitDefaultDispatcher-worker-1,SLHandshakeException:Chainvalidationfailed。