8

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.

Badr At
  • 658
  • 7
  • 22

2 Answers2

12

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

Quinn
  • 7,072
  • 7
  • 39
  • 61
0

You can include urls without specifying http:// for urls. Example of website http://mywebsite.com and ip address http://192.168.1.1, you can write like below:

<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">mywebsite.com</domain>
        <domain includeSubdomains="true">192.168.1.1</domain>
    </domain-config>
</network-security-config>
Sagar Chapagain
  • 1,683
  • 2
  • 16
  • 26
  • If you look at the question, he said he already tried this, add had I but it wasn't the solution to the problem. – Quinn Aug 01 '19 at 20:33
  • @Quinn I had pointed out the mistake he had made and wrote the correct way. – Sagar Chapagain Aug 02 '19 at 00:55
  • @SagarChapagain we were forced to deliver the functionnality asap, so we have switched to the main domain, i didn't tested what you mentionned, if that works for someone else, i'll mark it as the correct answer. thank you – Badr At Aug 02 '19 at 12:04