Android emulator connecting to 10.0.2.2 is not working

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

Android emulator connecting to 10.0.2.2 is not working
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>

About Ashis Biswas

A web developer who has a love for creativity and enjoys experimenting with the various techniques in both web designing and web development. If you would like to be kept up to date with his post, you can follow him.

Leave a Comment