未为客户端启用改造 CLEARTEXT 通信 [英] Retrofit CLEARTEXT communication not enabled for client

查看:86
本文介绍了未为客户端启用改造 CLEARTEXT 通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的项目中使用 Retrofit 库,但似乎 Retrofit 阻止了非 https 请求.我尝试在 Manifest.xml

i am working with Retrofit library on my project, but it seems that Retrofit block non https requests. I tried by adding in the application tag in Manifest.xml

android:usesCleartextTraffic="true"

但是没有用,我还尝试了另一种解决方案,在 res/xml 下添加一个安全配置文件:

but didn't work, i also tried another solution by adding under res/xml a security confing file:

<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">http://my subdomain/</domain>
    </domain-config>
</network-security-config>

并将其链接到 Manifest.xml 中的 application 标记中:

and link it in application tag in the Manifest.xml :

android:networkSecurityConfig="@xml/network_security_config"

两个解决方案都不起作用.我怎样才能避免这个错误?

both of the solution didn't work. how can i avoid this error ?

注意:当我使用 https 请求进行测试时,我的代码工作正常,并且出于测试目的,我们在使用 http 的子域中工作.

NB: my code works fine when i test with https request, and for testing purposes we are working in a subdomain which use http.

推荐答案

刚遇到这个确切的问题,不确定您的解决方案是否相同.但就我而言,我使用 okhttp3 作为我的 http 客户端,在构建我的客户端时,我必须像这样指定连接规范:

Just was having this exact problem, not sure if the solution for you will be the same. But in my case I was using okhttp3 as my http client, and when building my client I had to specify the connection specs like so:

val specs = listOf(ConnectionSpec.CLEARTEXT, ConnectionSpec.MODERN_TLS)

client.connectionSpecs(specs)

以前我只设置 MODERN_TLS,所以为了让我的库接受 http 连接,我必须添加 CLEARTEXT 规范

Previously I was only setting MODERN_TLS, so in order to allow my library to accept http connections I had to add the CLEARTEXT spec

这篇关于未为客户端启用改造 CLEARTEXT 通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆