Lots of android developers test their android app from the local server. Generally, they use a different type of android library, for example, Volley library to connect the app with localhost. Android emulator support 10.0.2.2 to connect localhost.
Sometimes developers face some connecting issue with localhost whenever they make a request to server i.e
com.android.volley.NoConnectionError: java.io.IOException: HTTP traffic to 10.0.2.2 not permitted
But if we try to send an HTTP request to other URLs except 10.0.2.2, it is working fine, no problem there.
Generally, 127.0.0.1 is for localhost, but in the case of an android emulator, it uses 10.0.2.2 as localhost.
So how to fix HTTP traffic to 10.0.2.2 not permitted?
So we need to add one more line to our Android manifest file i.e
android:usesCleartextTraffic="true"
This usesCleartextTraffic
attribute introduced by Android 6.0 (Marshmallow). By default, this attribute value is false. We need to set it to true from the manifest file. So here is what the code looks like.
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:usesCleartextTraffic="true"
-------------------
>
</application>
</manifest>