[SOLVED] Android: Cannot send data to the server. CLEARTEXT communication to not permitted by network security policy

When you try to connect with a server via HTTP, you got the exception like below

Cannot send data to the server
    java.net.UnknownServiceException: CLEARTEXT communication to 192.168.1.175 not permitted by network security policy
        at 
...
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:200)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:914)
W/vbcc-tag: Cannot send data to the server

The cause of title problem is that it’s required to add special security policy if the application connects to the server via HTTP instead of HTTPS protocol.

Solution #1: Use HTTPS instead of HTTP

Using encrypted transfer protocol is recommended way of communication between android application and server

Solution #2: Add exception to the security policy

Add src\main\res\xml\network_security_config.xml with the following content:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">your domain or ip address</domain>
    </domain-config>
</network-security-config>

Then register policy file in AndroidManifest.xml by setting the android:networkSecurityConfig attribute.

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:networkSecurityConfig="@xml/network_security_config"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
If the article was helpful to you, you can support me by:

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Opeyemi Abdulsalamliu Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
liu
Guest
liu

it works ,thanks a lot

Opeyemi Abdulsalam
Guest
Opeyemi Abdulsalam

Works well, thank you. But what is the best approach to solving the problem.

Close Menu