Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable connect to a HTTP URL in Android 10

I am using a http://something API for the login process. But could't get a hit on API in Android 10. For rest of the version, the API is working fine. First of all, I was getting SocketTimeoutException. Then I tried following solutions.

1) Added below attribute to <application> in Manifest.

android:usesCleartextTraffic="true"

Result: Still getting SocketTimeoutException.

2) Then I added networkSecurityConfig:

<network-security-config>
<domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">project.dev.company:6001/</domain>
    <trust-anchors>
        <certificates src="system"/>
    </trust-anchors>
</domain-config>
</network-security-config>

Result : UnknownServiceException : CLEARTEXT communication to project.dev.company:6001/ is not permitted by network security policy.

3) Also tried permitting CLEARTEXT in <base-config>. Still getting SocketTimeoutException.

How can I permit my app to access a HTTP connection from Android 10? I am using Retrofit2 for network calling.

like image 232
noob-Sci-Bot Avatar asked Jan 19 '26 04:01

noob-Sci-Bot


1 Answers

Use this codes in your xml file

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
   <domain-config cleartextTrafficPermitted="true">
      <domain includeSubdomains="true">project.dev.company</domain>
   </domain-config>
</network-security-config>

and use this codes in manifest:

android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_configuration"

dont need any "/" and port in domain,just use like me

Edited:

you can use IP of your api host,like this:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
      <domain includeSubdomains="true">192.168.1.1</domain>
   </domain-config>
</network-security-config>
like image 110
alireza daryani Avatar answered Jan 21 '26 21:01

alireza daryani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!