[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

4.6 13 votes
Article Rating
Subscribe
Notify of
guest
23 Comments
oldest
newest most voted
Inline Feedbacks
View all comments
liu
liu
1 year ago

it works ,thanks a lot

Opeyemi Abdulsalam
Opeyemi Abdulsalam
1 year ago

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

hicall
1 year ago

thanks bro

Lloyd
Lloyd
1 year ago

Salute……

ricky robiansyah
ricky robiansyah
1 year ago

how be set the domain if value is dynamic?

Trees
Trees
1 year ago

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

rrrvvv
rrrvvv
1 year ago

Great. was help

jack
jack
1 year ago

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

Mahesh
Mahesh
1 year ago

Perfect! Thank you!

Akash
Akash
11 months ago

Working well… Thanks!

Karthikmani
Karthikmani
11 months ago

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

pokkrong
pokkrong
10 months ago

Second solution work for me, thank you

Aldo Cortes
Aldo Cortes
9 months ago

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

Yogesh
Yogesh
9 months ago

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

raj
raj
9 months ago
Reply to  Yogesh

i insert this code to network_security_config.xml and word:

Mohan Dhakal
Mohan Dhakal
9 months ago

i got the error saying handshake failed after trying second solution. what should i do now?

Vaibhav
Vaibhav
9 months ago

I have applied the same solution but still I am not getting data from server.

Yoandrinho
Yoandrinho
8 months ago

I tried both solutions and I still get “CLEARTEXT communication to (…elasticbeanstalk.com) not permitted by network security policy”
I am using Retrofit to consume an api deployed in AWS ElasticBeanstalk.

taha
taha
7 months ago

Thanks

Lucho
Lucho
6 months ago
Reply to  taha

localhost don’t work please help me

Jay
Jay
4 months ago

2nd option worked for me. Do not put “http://” in … just the domain.