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.
- 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).
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
192.168.0.100. Then, you need to connect to this IP address and call your API like the following.
Make sure you are accepting requests to the port
8000in your PC. Check your Firewall configuration if it is blocking any incoming requests to the
8000port. If it is found blocking, then please allow an incoming request to the
8000port 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 10.0.2.2 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
- on Linux the one-liner
ifconfig | grep "inet " | grep -v 127.0.0.1will yield only the important stuff
- there's a bunch of suggestions on how to have a similar output on Windows
- on Linux the one-liner
- there's going to be a bunch of IP's
- try all of them (except the forementioned
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 (https://www.google.de/search?q=myip)
- 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 xip.io or ngrok.
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:
The above ip command can be abbreviated to:
If you still prefer to use
ifconfig as part of your daily sys admin routine, you can easily install it as part of the
apt-get install net-tools
Reference is here
Connect to localhost rest api from android mobile device
Resolved the issue:-
No need to change any firewall settings
Connect the mobile device and api running machine via the same network.
Use cmd-->ipconfig on windows to check ip address of api code running machine and "Wireless Lan Adapter Wi-Fi" (IPv6) worked for me.
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
Open your service URL(take from your browser) with prefix: ngrok http --host-header=rewrite
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.
Note: Url structure should be like this
public const string BaseUri = "https://8c56892f.ngrok.io/"; followed by
/ in last. Try not to append unnecessary /, in command prompt.
By using this method you can debug services from one Visual Studio to another.
Your URL will be active until you close command prompt.
In browser or command prompt track your requests status like Ok, not found etc.
For more information visit this https://www.c-sharpcorner.com/article/exposing-local-web-server-to-internet-using-ngrok/
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.