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
it works ,thanks a lot
Works well, thank you. But what is the best approach to solving the problem.
thanks bro
Salute……
how be set the domain if value is dynamic?
Thanks! I needed to do this after upgrading the support libraries in my android project.
Great. was help
Hi
Is it possible to explain more about first solution :”Use HTTPS instead of HTTP” ?
thanks in advace.
Perfect! Thank you!
Working well… Thanks!
I am also Put IP But after run network security config file comes default my changes are removed.what will do?
Second solution work for me, thank you
Thanks! the second one solved my problem! Good work!
Hi ,
I am facing the same problem for Ionic application, I made the changes but still, it is not working
i insert this code to network_security_config.xml and word:
i got the error saying handshake failed after trying second solution. what should i do now?
Check src\main\res\xml\network_security_config.xml again. Probably you put wrong value inyour domain or ip address … Here is an exemple for lolcalhost:
localhost
I have applied the same solution but still I am not getting data from server.
Please attach your files.
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.
Thanks
localhost don’t work please help me
2nd option worked for me. Do not put “http://” in … just the domain.
work for me, just insert ur IP or Domain wthout http//