ip地址从本地db获取数据

时间:2012-09-08 12:51:01

标签: android database

  

可能重复:
  How to get the Android Emulator's IP address?

我编写了一个Android应用程序来从我在安装Android模拟器的PC上创建的mysql数据库中获取数据。什么是我要设置到http post请求以获取访问权限的IP地址?

提前致谢

1 个答案:

答案 0 :(得分:0)

只需输入以下代码行即可获取当前模拟器的IP地址。

public 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(LOG_TAG, ex.toString());
}
return null;
}

它将在字符串的for中返回模拟器的IP地址。