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

7

401

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?

Anathematize answered 3/4, 2011 at 9:56 Comment(1)
P
88

Thanks, @lampShaded for your answer.

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

enter image description here

Protoplasm answered 10/4, 2019 at 6:26 Comment(1)
This one is working for me. Great.Balfore
S
945

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 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.

Severin answered 10/6, 2011 at 18:31 Comment(7)
Nice though this isn't useful for e.g. oauth though where a clientID is only valid for localhostPuritan
10.0.2.2 not working for me in case of https localhost, do i have to use different ip in case of https?Logjam
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.Recall
Is there any possibility to use siteurl/siteHost instead of ip, i can not use ip?Oribel
This is probably the best solution and it doesn't require proxy configuration on AVD (https://mcmap.net/q/86497/-how-do-you-connect-localhost-in-the-android-emulator-duplicate 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>.Outreach
what in case when one wants to access the localhost along with the subdomain?Solfa
10.0.2.2 works for me.Runner
B
99

Use 10.0.2.2 for default AVD and 10.0.3.2 for Genymotion

Brendabrendan answered 12/10, 2015 at 19:19 Comment(3)
Is this answer relevant anymore?Paragrapher
Yes, still working by the end of 2023. Also don't forget to add port after the 10.0.2.2Surveying
I forgot to add the port and pulling out of my hair saying why it's not working 😄Summerly
P
88

Thanks, @lampShaded for your answer.

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

enter image description here

Protoplasm answered 10/4, 2019 at 6:26 Comment(1)
This one is working for me. Great.Balfore
T
35

This is what finally worked for me.

  • Backend running on localhost:8080
  • Fetch your IP address (ipconfig on Windows)

enter image description here

  • 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 enter image description here

  • 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)

Tithe answered 6/12, 2019 at 16:58 Comment(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.Outreach
C
23

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

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

Chose answered 27/3, 2020 at 0:54 Comment(2)
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.xmlBarrack
Heled me just replace the ip address with your ip or domain nameHindoo
T
14

you should change the adb port with this command:

adb reverse tcp:8880 tcp:8880; adb reverse tcp:8081 tcp:8081; adb reverse tcp:8881 tcp:8881
Tanatanach answered 6/8, 2018 at 12:37 Comment(1)
my local server has a custom url. How do I port forward or reverse it?Dogooder
R
8

Instead of giving localhost give the IP.

Rao answered 3/4, 2011 at 10:8 Comment(3)
What IP? I have no public IP as I work in local server, did you mean loopback ip 127.0.0.1 ?Unvoiced
Fetch your IP address (ipconfig on Windows), here IP means IPv4 like 192.168.100.4Monied
what IP do you mean? please be specificHarrietteharrigan

© 2022 - 2024 — McMap. All rights reserved.