Cannot Connect to Localhost API from Android App

Cannot connect to localhost API from Android app

If you are testing your application from a real android device then you need to put the IP address of your PC while you are trying to connect to your Django server through APIs. And yes, you need to be in the same network as well. So you need to check the following things.

  1. Make sure that the PC (where you are running the server) and the Android device (where you are testing your application) are in the same network (connected with same Wifi network maybe).
  2. Make sure you are connecting to the IP address of your PC where the server is running. For example, right now, the IP address of your PC is Then, you need to connect to this IP address and call your API like the following.
  3. Make sure you are accepting requests to the port 8000 in your PC. Check your Firewall configuration if it is blocking any incoming requests to the 8000 port. If it is found blocking, then please allow an incoming request to the 8000 port using the following.

    sudo ufw allow 8000/tcp

If there is nothing which is helping you, then please check your Android code to check if the API calling is okay. I would strongly recommend using Volley for API calls suggested in developers documentation.

Last, but not the least, please check if you have necessary permission in your AndroidManifest.xml file. You need to add the following permission to grant your application to use the internet.

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Android app unable to connect to localhost service

If you're running the android app on an emulator, you can just use the ip of By default, this refers to the host of the emulator.

If you're on a real device, use the real IP. This means the server either needs to be on the saw WIFI network AND the network firewall has to be set up to allow traffic to it, or you need a publicly addressable IP address. Most router setups NAT by default, so you'll have to poke a hole in it.

Also, make sure the server is using the right IP address and isn't just bound to localhost.

How can I access my localhost from my Android device?

USB doesn't provide network to mobile device.

If both your desktop and phone are connected to the same WiFi (or any other local network), then use your desktop IP address assigned by the router (not localhost and not

To find out the IP address of your desktop:

  • type into the command line ipconfig (Windows) or ifconfig (Unix)

    • on Linux the one-liner ifconfig | grep "inet " | grep -v will yield only the important stuff
    • there's a bunch of suggestions on how to have a similar output on Windows
  • there's going to be a bunch of IP's
  • try all of them (except the forementioned localhost and

If your phone is connected to the mobile network, then things are going to be harder.

Either go hardcore:

  • first find out your router external IP address (
  • then, on the router, forward some port to <your desktop IP>:<server port number>
  • finally use the external IP address and forwarded port

Otherwise use something like or ngrok.

NOTE: The ifconfig command has been deprecated and thus missing by default on Debian Linux, starting from Debian stretch. The new and recommended alternative for examining a network configuration on Debian Linux is ip command. For example to use ip command to display a network configuration run the following:

ip address

The above ip command can be abbreviated to:

ip a

If you still prefer to use ifconfig as part of your daily sys admin routine, you can easily install it as part of the net-tools package.

apt-get install net-tools

Reference is here

Connect to localhost rest api from android mobile device

Resolved the issue:-

  1. No need to change any firewall settings

  2. Connect the mobile device and api running machine via the same network.

  3. Use cmd-->ipconfig on windows to check ip address of api code running machine and "Wireless Lan Adapter Wi-Fi" (IPv6) worked for me.

  4. Mention full url when connecting via retrofit.

    Retrofit retrofit = new Retrofit.Builder()

Android emulator not connecting to localhost api

There is a simple workaround to connect Emulator & Simulator to localhost API's which I am using.

Download third party software NgRok for Windows at any specific location of your PC. It just contains one executable file called ngrok.exe.
Now execute your service on your preferred browser. Than follow below steps.

  • Open your ngrok location on command prompt

    enter image description here

  • Open your service URL(take from your browser) with prefix: ngrok http --host-header=rewrite

    enter image description here

  • Now go to your browser where you have execute your service open another tab & type localhost:4040 than press enter -->Go to status there you will find your public URL which you can use in your mobile application.

    enter image description here

Note: Url structure should be like this public const string BaseUri = ""; followed by / in last. Try not to append unnecessary /, in command prompt.


  1. By using this method you can debug services from one Visual Studio to another.

  2. Your URL will be active until you close command prompt.

  3. In browser or command prompt track your requests status like Ok, not found etc.

For more information visit this

Hope it help you.

Timeout Error when connecting localhost api with Android Studio on Real devices through USB

I disabled the Firewall and everything is working perfectly.

Related Topics

Leave a reply