How to connect to my http://localhost web server from Android Emulator
Asked Answered
O

20

614

What can I do in the Android emulator to connect it to my localhost web server page at http://localhost or http://127.0.0.1?

I've tried it, but the emulator still takes my request like a Google search for localhost or worse it says that it didn't found the page while my web server is normally running.

Onto answered 27/4, 2011 at 15:1 Comment(1)
This question is about the Android Emulator and has solutions applicable to that case alone. If you face this problem when using a physical Android device, please see instead #4780463Margarito
P
1033

The localhost refers to the device on which the code is running, in this case the emulator.

If you want to refer to the computer which is running the Android simulator, use the IP address 10.0.2.2 instead.

enter image description here

You can read more from here.

Parliamentary answered 27/4, 2011 at 15:13 Comment(9)
why referring to teh other question? simply add the Link Android emulator NetworkingSardanapalus
@primpap :Can I use the machine name instead of the ip address??Elana
@primpap this is not working with Mobile debugging. Is there some other ? Or i am doing mistake somewhere because 10.0.2.2 is working on emulator but not working when i connect mobile for usb debugging. ?Dragonfly
what if , I am using mobile deviceRockefeller
I think this is not working anymore, I noticed an error when the emulator startedPostglacial
This solution only works on emulators, as asked in the specific question here. Those debugging on a physical Android device will have to use the server's actual IP address or name, they can't use this special emulator-only alias. This answer is good - but readers need to be careful that their situation is one in which it is applicable.Margarito
For flutter, it is still the same: 10.0.2.2Cannula
@Marvin Emil Brach The link doesn't anymore. Currently it is: developer.android.com/studio/run/emulator-networkingMotorcade
@Ng Sek Long Why should it differ? Don't you use the same emulator(s) with Flutter?Motorcade
F
177

Use 10.0.2.2 for default AVD and 10.0.3.2 for .

Foulmouthed answered 20/7, 2014 at 18:25 Comment(3)
So do we just add 10.0.3.2 in our host file?Cellarage
if you're on nativescript, there's an extra step: #56816134Giggle
I'm running my app in my mobile... Is it the same 10.0.2.2? @FoulmouthedMascon
O
35

You can actually use localhost:8000 to connect to your machine's localhost by running below command each time when you run your emulator (tested on Mac only):

adb reverse tcp:8000 tcp:8000

Just put it to Android Studio terminal.

It basically sets up a reverse proxy in which a http server running on your phone accepts connections on a port and wires them to your computer or vice versa.

Orison answered 6/6, 2020 at 10:55 Comment(6)
This is best option if you use ssl and openAuth.Fart
Also best answer if you have CORS issues, thanks a lot!Jobless
For those like me who use Android Studio but can't call adb, it IS installed by Android Studio, but you need to add it in your PATH manually, it's not done automatically by Android Studio for some reason. See the following for the paths on Windows, Linux and MacOS: medium.com/androiddevelopers/help-adb-is-not-found-93e9ed8a67eeLazybones
I tried every other option but this was the only thing that worked.Overreact
And what's the point of all this?Motorcade
Thanks, after trying many solutions here. This is a solution that works for me.Rift
P
34

I used 10.0.2.2 successfully on my home machine, but at work, it did not work. After hours of fooling around, I created a new emulator instance using the Android Virtual Device (AVD) manager, and finally the 10.0.2.2 worked.

I don't know what was wrong with the other emulator instance (the platform was the same), but if you find 10.0.2.2 does not work, try creating a new emulator instance.

Petra answered 19/3, 2012 at 20:55 Comment(1)
Unfortunatelly it doesn't work to me even after creating new AVD instance. What do you suggest? To reinstall everything?Fourscore
C
32

Try http://10.0.2.2:8080/ where 8080 is your port number. It worked perfectly. If you just try 10.0.2.2 it won't work. You need to add port number to it. Also if Microsoft IIS has been installed try turning off that feature from control panel (if using any windows os) and then try as given above.

Caducity answered 5/9, 2013 at 21:17 Comment(4)
What if the pages are hosted on IIS?Telemeter
@Marimba just figure out what port your IIS server serves on, then access 10.0.2.2:your_iis_portCrippling
this is a completed string and the only worked on my android 8.1 emulatorTagore
As everywhere else it means 80 if you don't specify the port. If your server is listening on another port then it is obvious that you have to specify it.Motorcade
L
10

according to documentation:

10.0.2.2 - Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)

check Emulator Networking for more tricks on emulator networking.

Lipp answered 9/11, 2015 at 11:7 Comment(0)
A
7

For My Mac OS mountain Lion device :

http://10.0.2.2:8888

Works perfect !

Amenable answered 2/2, 2014 at 1:15 Comment(1)
The port doesn't matter. It only "works" if you have something listening there. The question was about 127.0.0.1 which means port 80.Motorcade
L
7

Despite reading all the answers here and elsewhere, I have lost several hours trying to debug this issue, as the address 10.0.2.2 did not work, even in Chrome browser. If the same is happening to you, here is a step-by-step guide to try to debug and hopefully fix your issue.

Check emulator gateway is 10.0.2.2

Inside the emulated Android, go to Settings > WiFi, check if it is connected to AndroidWiFi hotspot (which represents your host computer), and then click on Advanced at the bottom, then check the Gateway address: it should point to 10.0.2.2 . If not, then you have another issue, maybe changing proxy settings can fix your issue, see here how to do that with Android Studio since 2022, as the proxy setting is now hidden away: How to configure proxy in emulators in new versions of Android Studio?

Check if your server is accessible from your host computer

Simply open a web browser and type http://localhost:<port> to see if your local web app is accessible. If not, then you likely have an issue with your local server parameters.

Check if your server is accessible from the emulator

Open Chrome browser, and point it to http://10.0.2.2:<port> (for genymotion, replace with http://10.0.3.2:<port>). If your web app shows up, great, you're done. If not, then test the other steps below to pinpoint the root issue.

Test with another server

In case your web app can be accessed from your host computer, but not inside the emulator, the root cause can be that your local server is restricting access to some interfaces for some reason, likely for security reasons.

To check this, try to use another server, just a simple HTTP server will do, such as http-server with nodejs, or python -m http.server 8000 with Python 3.

Then, try to access this simple server from your emulator's Chrome browser, eg, http://10.0.2.2:8000. If it works, then this confirms that your local server is restricting access to some interfaces. You need to read your local server's documentation to broaden permissions.

For example, in my case, my server was angular-cli (AngularJS), which by default restricts serving only to localhost. To make it work, I had to use ng serve --disable-host-check --host 0.0.0.0 instead of just ng serve, as suggested in this other question. The --host 0.0.0.0 instructs the webserver to serve all interfaces. Similar arguments can be provided to most webservers.

An alternative might be to disable some unused adapters, especially virtual ones such as VPNs.

Your Android app permissions to cleartext

Now, your web app should be accessible from inside the emulator, using Chrome app, with the URL http://10.0.2.2:<port>. The last piece of the puzzle is to add permissions in your Android app to access 10.0.2.2 and especially cleartext if your local webserver is not equipped with a SSL certificate (the most likely scenario for a local development webserver - just check if https://localhost:<port> works or only http://localhost:<port> from the host computer). This will allow your Android app to access your local webserver, just like Chrome does.

Adding specific permissions to access cleartext (ie, http://) from your Android app is necessary since Android 9 (API 28) upwards. There are several ways to configure your Android app to add this permission, see: https://mcmap.net/q/35675/-android-8-cleartext-http-traffic-not-permitted

Conclusion

Accessing the host from the Android emulator can be tricky, but by careful step-by-step debugging, it can be possible to overcome the issue in most cases.

This tutorial only covers the issue of accessing/reaching a local webserver on the host computer from inside an Android emulator, but once this is fixed, the webapp may remain dysfunctional, even if reachable. One example is to experience an infinite loading loop. To debug further issues once reachability is resolved, you can use chrome://inspect in the Chrome Browser to attach to the Android WebView inside the Android emulator and live debug it as if it was rendered on your computer.

A last alternative, probably faster, is to get a paid subscription to services such as ngrok, but the free version is useless as they necessarily open the webapp in a web browser, outside of your Android app's webview.

Lazybones answered 18/1, 2023 at 6:2 Comment(0)
L
5

If you using Android Emulator :

You can connect to your Pc localhost by these IPs : 10.0.2.2:{port of your localhost} => if you set your machine port in xamp you must use that port . In my case 10.0.2.2:2080

enter image description here

enter image description here

Also you can use your network adapter IP .In CMD write ipconfig and find your adapter ip address :

enter image description here

enter image description here

If emulator can not connect to this IPs close the emulator an open it by cold boot from AVD Manager :

enter image description here

If you using Genymotion :

You can connect to machine localhost by this IP : 10.0.3.2:{port number} Or your adapter IP address as I explained above: in my case : 192.168.1.3:2080

Laure answered 13/12, 2020 at 17:53 Comment(0)
C
3

I needed to figure out the system host IP address for the emulator "Nox App Player". Here is how I figured out it was 172.17.100.2.

  1. Installed Android Terminal Emulator from the app store
  2. Issue ip link show command to show all network interfaces. Of particular interest was the eth1 interface
  3. Issue ifconfig eth1 command, shows net as 172.17.100.15/255.255.255.0
  4. Begin pinging addresses starting at 172.17.100.1, got a hit on `172.17.100.2'. Not sure if a firewall would interfere but it didn't in my case

Maybe this can help someone else figure it out for other emulators.

Campbellbannerman answered 22/6, 2018 at 19:28 Comment(0)
S
3

Allowing PWA installation

First of all, install the Android debug bridge:

$ sudo apt install adb android-sdk-platform-tools-common

Start your Android emulator as usual, e.g.:

$ ~/Android/Sdk/emulator/emulator -avd Pixel_3a_API_30_x86

Only then, configure a reverse proxy on the bridge of the Android emulator that will forward localhost HTTP requests to the appropriate port (e.g. 8000) of the localhost server running on your host computer and vice versa:

$ adb reverse tcp:8000 tcp:8000

A progressive web application (PWA) being served on localhost:8000 or 127.0.0.1:8000 will be installable and connect to its service-worker.js. Whereas PWA installation is not allowed from IP address 10.0.2.2.

Caveat: adb reverse tcp:8000 tcp:8000 needs to be reissued after each Android emulator evocation.

Hence, a bash script to launch an Android emulator, followed by a reverse proxy, would look like this:

#!/usr/bin/env bash

$HOME/Android/Sdk/emulator/emulator -avd Pixel_3a_API_30_x86 > /dev/null 2>&1

adb reverse tcp:8000 tcp:8000
Spaniel answered 6/4, 2021 at 16:17 Comment(1)
I can't see that the topic is about installing progressive web applications.Motorcade
A
2

The accepted answer is correct, but didn't work in my case. I had to create the virtual device with the company VPN-client on the host machine turned off. This is quite understandable as many company networks use adresses starting with 10 (private network range), which could interfere with the special address 10.0.2.2

Awad answered 28/3, 2022 at 11:5 Comment(1)
No. There's nothing understandable about that. It doesn't matter how the address starts. As long as it doesn't start with 10.0.2. there isn't any "interference".Motorcade
G
1

If you are in windows you can go to simbol system and write ipconfig and check what ip is assigned to your machine.

Giannini answered 23/2, 2014 at 12:39 Comment(1)
This is not necessary when debugging from an emulator, as you can use the special alias for the host loopback interface cited in the other answers. However if debugging from a physical android device, then yes, you need to determine the actual address of your test server and use that.Margarito
E
1

FOR ANYONE TRYING TO REACH A LOCAL IIS Server (ASP.NET)

For me, the accepted answer was not enough. I had to add a binding for 127.0.0.1 in the applicationhost.config, which was at the root of my ASP.NET solution.

Eadmund answered 10/4, 2022 at 0:31 Comment(0)
A
0

I do not know, maybe this topic is already solved, but when I have tried recently do this on Windows machine, I have faced with lot of difficulties. So my solution was really simple. I have downloaded this soft http://www.lenzg.net/rinetd/rinetd.html followed their instructions about how to make port forwarding and then successfully my android device connected to make asp.net localhost project and stopped on my breaking point.

my rinetd.conf file:

10.1.1.20 1234 127.0.0.1 1234
10.1.1.20 82 127.0.0.1 82

Where 10.1.1.20 is my localhost ip, 82 and 1234 my ports Also I have craeted bath file for easy life yournameofbathfile.bat, put that file inside rinedfolder. My bath file:

rinetd.exe -c rinetd.conf

After starting this soft, start your aps.net server and try to access from android device or any device in your local network(for example Computer ABC starts putty) and you will see that everything works. No need to go to router setting or do any other complicated things. I hope this will help you. Enjoy.

Astolat answered 9/3, 2017 at 9:53 Comment(1)
There is nothing "simple" about your solution. Using 10.0.2.2 provided by Google for this purpose is simple. To know if a topic is solved there is a green check mark at the accepted answer. Which existed almost 6 years before you answered.Motorcade
T
0

2023 answer:

I have an express app listening on localhost:5001, and a production api at my-real-api.com so I use:

// BASE_URL.js
import Constants from "expo-constants";
const { manifest } = Constants;

const PORT=5001
export const BASE_URL =
  (typeof manifest?.packagerOpts === `object` &&
  !!manifest.debuggerHost &&
  manifest.packagerOpts.dev)
    // dev server
    ? `http://${manifest.debuggerHost.split(`:`).shift()}:${PORT}` + 
    // production server
    : "https://my-real-api.com";

and then everywhere else:

import { BASE_URL } from "../BASE_URL"
import axios from "axios"
const my_data = await axios.get(BASE_URL+"/some/path")
Truckle answered 19/3, 2023 at 17:28 Comment(1)
I don't see the connection to the topic. In 2023 you still just have to use 10.0.2.2 in the emulator.Motorcade
R
0

In my case i was running a local server, and working on a flutter app which was targeting Android, iOS, Web. This might help you. - Here

Rudy answered 11/11, 2023 at 11:47 Comment(0)
S
0

The Android emulator operates behind a virtual router, which prevents direct access to the network interface of the local computer.

To circumvent this issue, the web server should be started with the IP4 address of the local machine (ipconfig /all). Then, the emulator can access the server using the same IP address instead of using 'localhost'.

Sisterly answered 19/12, 2023 at 16:1 Comment(0)
H
-1

Another workaround is to get a free domain from no-ip.org and point it to your local ip address. Then, instead of using http://localhost/yourwebservice you can try http://yourdomain.no-ip.org/yourwebservice

Hynda answered 30/4, 2014 at 9:24 Comment(0)
G
-2

I know this is old, but if you find that 10.0.2.2 is not working as the computer IP, follow these instructions to find it

Gridley answered 14/7, 2021 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.