Android emulator connecting to 10.0.2.2 is not working

Lots of android developer 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 others URL except 10.0.2.2, it is working fine, no problem there.

Generally, 127.0.0.1 is for localhost, but in 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 the code look like.

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:usesCleartextTraffic="true"
        -------------------
    >
    </application>
</manifest>

Spread the love
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x