Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Calling all who code. Take the 2023 Developer Survey.

How do you connect localhost in the Android emulator? [duplicate]


Asked 12 years, 1 month ago Modified 1 year, 5 months ago Viewed 254k times

This question already has answers here:

355 How to connect to my http://localhost web server from Android Emulator (18 answers)

Closed 2 years ago.

I have made a php script inside localhost and I am connecting that with httpClient but I am getting a
problem.

Please tell me how can I connect to a php file at localhost from the emulator?

android-emulator localhost httpconnection androidhttpclient

Share Improve this question Follow edited Apr 16, 2018 at 23:36 asked Apr 3, 2011 at 9:56
shim Dharmendra
9,122 12 69 108 33k 22 86 129

Sorted by:
7 Answers
Highest score (default)

Use 10.0.2.2 to access your actual machine.

As you've learned, when you use the emulator, localhost ( 127.0.0.1 ) refers to the device's own loopback
862
service, not the one on your machine as you may expect.

You can use 10.0.2.2 to access your actual machine, it is an alias set up to help in development.

Share Improve this answer Follow edited Aug 17, 2015 at 16:27 answered Jun 10, 2011 at 18:31
Joshua Pinter lampShaded
44.5k 23 240 244 8,925 2 17 12

7 Nice though this isn't useful for e.g. oauth though where a clientID is only valid for localhost – Dominic Jul 27,
2017 at 15:14

16 10.0.2.2 not working for me in case of https localhost, do i have to use different ip in case of https? – Manu Nov 15,
2018 at 7:15

6 But you need to change API from "localhost:port" to "127.0.0.1:port" first. after that, Use 10.0.2.2 to access your actual
machine. – binhtruong.it Dec 9, 2018 at 11:49

Is there any possibility to use siteurl/siteHost instead of ip, i can not use ip? – Meenakshi Khandelwal Apr 22, 2020 at
16:09

2 This is probably the best solution and it doesn't require proxy configuration on AVD
(stackoverflow.com/a/59217370/6422750 actually doesn't either). You can access your endpoints defined on the
server via http://10.0.2.2:<port_your_server_runs_on>/<path> . – Theta Nov 27, 2020 at 12:26
Use 10.0.2.2 for default AVD and 10.0.3.2 for Genymotion

86 Share Improve this answer Follow edited Nov 15, 2021 at 17:12 answered Oct 12, 2015 at 19:19
dKen S raj
3,069 1 27 37 901 6 6

1 Is this answer relevant anymore? – Kalnode Jan 5 at 16:25

Thanks, @lampShaded for your answer.

74 In your API/URL directly use http://10.0.2.2:[your port]/ and under emulator setting add the proxy
address as 10.0.2.2 with the port number. For more, you can visit:
https://developer.android.com/studio/run/emulator-networking.html

Share Improve this answer Follow edited Apr 11, 2019 at 8:17 answered Apr 10, 2019 at 6:26
Md Imran Choudhury
9,105 4 60 59

This one is working for me. Great. – Rabindra Khadka Jul 3, 2022 at 10:21

This is what finally worked for me.


32 Backend running on localhost:8080

Fetch your IP address (ipconfig on Windows)

Configure your Android emulator's proxy to use your IP address as host name and the port your
backend is running on as port (in my case: 192.168.1.86:8080

Have your Android app send requests to the same URL (192.168.1.86:8080) (sending requests to
localhost, and http://10.0.2.2 did not work for me)

Share Improve this answer Follow answered Dec 6, 2019 at 16:58


Chris Neve
2,098 2 20 32

1 This works for me too but I believe proxy is not necessary - logs on my Flask server showed that when proxy was
configured, AVD generated plenty of weird and unnecessary GETs and CONNECTs that resulted in 404s. Switching
back to Use Android Studio HTTP proxy settings fixed it and now the server receives only valid requests that I
explicitly want. One more thing you could do for your convenience is assign a static IP address to your machine, for
instance via your router settings so that DHCP won't be able to change it. – Theta Nov 27, 2020 at 12:12

Thanks to author of this blog: https://bigdata-etl.com/solved-how-to-connect-from-android-emulator-to-


application-on-localhost/
20
Defining network security config in xml
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">10.0.2.2</domain>
</domain-config>
</network-security-config>

And setting it on AndroidManifest.xml

<application
android:networkSecurityConfig="@xml/network_security_config"
</application>

Solved issue for me!

Please refer: https://developer.android.com/training/articles/security-config

Share Improve this answer Follow answered Mar 27, 2020 at 0:54
Prakash
4,429 30 42

What is the filename for the security config xml? The blog link is no longer up. Edit: it's in
res/xml/network_security_config.xml – Velizar Hristov Apr 25, 2022 at 12:07

Heled me just replace the ip address with your ip or domain name – Deepak gupta Sep 20, 2022 at 13:13

you should change the adb port with this command:

11 adb reverse tcp:8880 tcp:8880; adb reverse tcp:8081 tcp:8081; adb reverse tcp:8881 tcp:8881

Share Improve this answer Follow answered Aug 6, 2018 at 12:37


jsina
4,303 1 30 28

my local server has a custom url. How do I port forward or reverse it? – Rohit Singh Feb 2, 2022 at 21:12

Instead of giving localhost give the IP.

8 Share Improve this answer Follow answered Apr 3, 2011 at 10:08


Viren Pushpanayagam
2,397 6 35 39

1 What IP? I have no public IP as I work in local server, did you mean loopback ip 127.0.0.1 ? – Sazzad Hissain Khan
Apr 1, 2020 at 15:25

2 Fetch your IP address (ipconfig on Windows), here IP means IPv4 like 192.168.100.4 – Muhammad Azam Jan 31, 2021
at 18:21

what IP do you mean? please be specific – Anas Dec 3, 2022 at 15:46

You might also like