Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 678
Badr At Avatar asked Feb 28 '19 16:02

Badr At


1 Answers

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

like image 130
Quinn Avatar answered Sep 19 '22 11:09

Quinn