[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. It means that you should configure server to use encrypted traffic. How? It depends on whtat exactly server do you use, but just google “server name https configuration”

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>
At the end… May I ask you for something?

If I helped you solve your problem, please share this post. Thanks to this, I will have the opportunity to reach a wider group of readers. Thank You

15
Leave a Reply

avatar
14 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
15 Comment authors
rajYogeshAldo CortespokkrongKarthikmani 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.

hicall
Guest

thanks bro

Lloyd
Guest
Lloyd

Salute……

ricky robiansyah
Guest
ricky robiansyah

how be set the domain if value is dynamic?

Trees
Guest
Trees

Thanks! I needed to do this after upgrading the support libraries in my android project.

rrrvvv
Guest
rrrvvv

Great. was help

jack
Guest
jack

Hi
Is it possible to explain more about first solution :”Use HTTPS instead of HTTP” ?
thanks in advace.

Mahesh
Guest
Mahesh

Perfect! Thank you!

Akash
Guest
Akash

Working well… Thanks!

Karthikmani
Guest
Karthikmani

I am also Put IP But after run network security config file comes default my changes are removed.what will do?

pokkrong
Guest
pokkrong

Second solution work for me, thank you

Aldo Cortes
Guest
Aldo Cortes

Thanks! the second one solved my problem! Good work!

Yogesh
Guest
Yogesh

Hi ,
I am facing the same problem for Ionic application, I made the changes but still, it is not working

raj
Guest
raj

i insert this code to network_security_config.xml and word:

Close Menu