Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 559
Dharmendra Avatar asked Apr 03 '11 09:04

Dharmendra


People also ask

How do I connect my Android emulator?

To start the Android Emulator and run an app in your project: In Android Studio, create an Android Virtual Device (AVD) that the emulator can use to install and run your app. In the toolbar, select the AVD that you want to run your app on from the target device drop-down menu. Click Run .

How do I access localhost from another device?

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:8080".

How do I access localhost?

To access the server from itself, use http://localhost/ or http://127.0.0.1/ . To access the server from a separate computer on the same network, use http://192.168.X.X where X.X is your server's local IP address. You can find the sever's local IP address (assuming it's Linux) by running hostname -I . 127.0.


7 Answers

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.

like image 75
lampShaded Avatar answered Oct 06 '22 01:10

lampShaded


Use 10.0.2.2 for default AVD and 10.0.3.2 for Genymotion

like image 22
S raj Avatar answered Oct 05 '22 23:10

S raj


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

like image 43
Md Imran Choudhury Avatar answered Oct 06 '22 00:10

Md Imran Choudhury


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)

like image 39
Chris Neve Avatar answered Oct 05 '22 23:10

Chris Neve


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

like image 35
Prakash Avatar answered Oct 06 '22 00:10

Prakash


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
like image 28
jsina Avatar answered Oct 05 '22 23:10

jsina


Instead of giving localhost give the IP.

like image 33
Viren Pushpanayagam Avatar answered Oct 06 '22 00:10

Viren Pushpanayagam