How do you connect localhost in the Android emulator?

Android EmulatorLocalhostHttpconnectionAndroidhttpclient

Android Emulator Problem Overview


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 Solutions


Solution 1 - Android Emulator

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.

Solution 2 - Android Emulator

Use 10.0.2.2 for default AVD and 10.0.3.2 for Genymotion

Solution 3 - Android Emulator

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

Solution 4 - Android Emulator

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)

Solution 5 - Android Emulator

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

Solution 6 - Android Emulator

Instead of giving localhost give the IP.

Solution 7 - Android Emulator

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDharmendraView Question on Stackoverflow
Solution 1 - Android EmulatorlampShadedView Answer on Stackoverflow
Solution 2 - Android EmulatorS rajView Answer on Stackoverflow
Solution 3 - Android EmulatorMd Imran ChoudhuryView Answer on Stackoverflow
Solution 4 - Android EmulatorChris NeveView Answer on Stackoverflow
Solution 5 - Android EmulatorPrakashView Answer on Stackoverflow
Solution 6 - Android EmulatorViren PushpanayagamView Answer on Stackoverflow
Solution 7 - Android EmulatorjsinaView Answer on Stackoverflow