Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...