7

I'm trying to write my own Android http server. It's quite OK but I have a problem with my AVD. I don't want to download my app to phone every time I want to test changes. I would like to connect to my app via AVD.
To get the IP address I'm using this function:

private String getLocalIpAddress() {


        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); }
                }
            }
        } catch (SocketException ex) {
            Log.e("ServerActivity", ex.toString());
        }
        return null;
    }

on my phone everything works but when I run my app on AVD it shows ip: 10.0.2.15 and I'm unable to connect to it.
Is there any way to connect to my app running on AVD?
If it matters my app uses port 8080.

Chenmunka
  • 685
  • 4
  • 21
  • 25
Gaski
  • 71
  • 1
  • 2

2 Answers2

7

Telnet into the device (assuming it is on port 5554):

telnet localhost 5554

At the Android console prompt use a redirect:

redir add tcp:8080:8080

Pointing your browser to 'http://127.0.0.1:8080/' should now send, and receive to the AVD.

Courtesy of: http://www.rhill.co.uk/?p=35

John
  • 71
  • 1
  • 2
0

While I don't know the answer to your problem directly, I do know that when connecting FROM an AVD TO your computer, you have to use 10.0.2.2 because your AVD is essentially behind another "router". It doesn't get a local lan IP from your router. See this question for more information. From this link, he quotes:

Each instance of the emulator runs behind a virtual router/firewall service that isolates it from your development machine's network interfaces and settings and from the internet. An emulated device can not see your development machine or other emulator instances on the network. Instead, it sees only that it is connected through Ethernet to a router/firewall.

Community
  • 1
  • 1
Jack
  • 9,156
  • 4
  • 50
  • 75