Accessing localhost:port from Android emulator
android, android-emulator, localhost, port
Solution
You can access your host machine with the IP address "`10.0.2.2`".
This has been designed in this way by the Android team. So your webserver can perfectly run at localhost and from your Android app you can access it via "`http://10.0.2.2:<hostport>`".
If your emulator must access the internet through a proxy server, you can configure a custom HTTP proxy from the emulator's Extended controls screen. With the emulator open, click More , and then click Settings and Proxy. From here, you can define your own HTTP proxy settings.
Problem
I'm running a web service on my local machine that runs at `localhost:54722`. I want to call the service from an app running in the Android emulator. I read that using `10.0.2.2` in the app would access localhost, but it doesn't seem to work with the port number as well. It says `HttpResponseException: Bad Request`.
Related problems
- How do you connect localhost in the Android emulator?
- How to enable external request in IIS Express?
- Android 8: Cleartext HTTP traffic not permitted
- How can I access my localhost from my Android device?
- Can you force Visual Studio to always run as an Administrator in Windows 8?
- How to allow all Network connection types HTTP and HTTPS in Android (9) Pie?
- sending json object to HTTP server in android
- Android: "Bad Request-Invalid Hostname" When accessing localhost from emulators