Http://10.0.2.2 works on an Android emulator, but not on an Android device used as an emulator

I connected my Android device via USB to use it as an emulator, if I access the URL from the eclipse emulator, it works, but the same thing, if I access the device from the device as an emulator, it gives a timeout error connectivity.

Are there any settings I need to change for this? or is there a possible solution for this?

thanks

+4
source share
2 answers

Here's what I needed to do to get the device to pick up a local service instance on my Windows machine running on localhost.

  • Turn on WiFi on my device and connect to the wireless network.
  • Run ipconfig at the command prompt.
  • Use an IPv4 address to connect to an Ethernet LAN (since I am connected via Ethernet)
  • Change base url in android app to use this url

Then my Android application could connect to the local service instance via this IP address, and not 10.0.2.2, which works when you run the application on the emulator. If someone was so addicted, you could extract these URLs and check if the application is running on an emulator or device, and then set the IP address correctly in the code. Hope this helps.

Edit the url for the above output - I created the ApplicationName.java file and declared this variable:

public static String ANDROID_DEVICE_ID = ""; 

Then set this variable in my initial action:

 ApplicationName.ANDROID_DEVICE_ID = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID); 

Then I copy this device id, and in my api class that calls the web service calls, I have the following code:

 private String getCategoriesUrl() { // TODO: Remove before production if (AgoraApplication.ANDROID_DEVICE_ID.equals("deviceIdString")) { _categoriesUrl = _deviceIp + _categoriesUrlSuffix; } else { _categoriesUrl = _emulatorIp + _categoriesUrlSuffix; } return _categoriesUrl; } 

Therefore, when I debug on a device or emulator, my application automatically uses the appropriate IP address for service calls.

+5
source

You make the assumption that the device likes the emulator. It definitely launches your application independently, without mimicking or mimicking it. You will need to configure the IP address on your computer IP address, which the phone can see from your network, cellular or Wi-Fi.

+2
source

Source: https://habr.com/ru/post/912756/


All Articles