2

In my application, I am trying to connect to a server(virtual machine) with specified host, but I keep getting the error "A server with the specified hostname could not be found", while when I connect the same server from my local machine through a browser or just ping, I am able to do that. I have modified hosts file on my local machine and also uploaded the same hosts file on android emulator. Does anyone have any pointers that can help me to resolve this issue?

yogsma
  • 10,142
  • 31
  • 97
  • 154
  • 1
    Check whether Internet Browser is able to open websites. If browser does says "offline message", then restart your emulator. It could fix your problem. – Ramesh_D May 29 '14 at 13:33
  • Internet browser is able to open the websites. – yogsma May 30 '14 at 13:48
  • What is that host file that your mentioning ? Can you show up that file – Ramesh_D Jun 01 '14 at 10:37
  • 1
    you should probably look for this link it may help you http://stackoverflow.com/questions/5806220/how-to-connect-to-my-http-localhost-web-server-from-android-emulator-in-eclips – Pankaj Jun 02 '14 at 16:28

1 Answers1

5

You will probably have to configure your virtual machine to port forward a port from your host machine to the virtual machine web server

  1. Configure virtual machine server: in my case Ubuntu in VirtualBox
    • Port forward via this link or use NAT port forwarding via the networks tab
  2. Ensure that the server is accessible in host machine
    • In host web browser (e.g. Chrome in my screenshot below), go to 10.0.2.2:configuredPort (in my case localhost:8181)
  3. Start the android emulator and go to 10.0.2.2:configuredPort
    • The special IP of 10.0.2.2 is taken from the Android docs and represents your host machine

My VirtualBox setup with the Android Emulator

Edit:

If the virtual machine is bridged on the network, it should be accessible via IP / hostname if you modify the Android hosts file.

127.0.0.1           localhost
192.168.0.123       virtualMachineBridgedAddress.com
10.0.2.2            hostMachineAddress.com
thewheat
  • 988
  • 7
  • 13