Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

android - Retrofit CLEARTEXT communication not enabled for client

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"

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>

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 ?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...