使用java在Android项目中获取计算机的IP地址
我使用的是ksoap2-android,我需要使用java获取IP地址,这样我就不必每次都手动输入。
  我的意思是IP地址是,例如,如果我使用命令shell执行ipconfig: 
  连接特定的DNS后缀。  : 
  链路本地IPv6地址。  。  。  。  。  :f0 :: ed2:e3bf:8206:44%13 
  IPv4地址。  。  。  。  。  。  。  。  。  。  。  :192.168.1.107 < - 这一个 
  子网掩码 。  。  。  。  。  。  。  。  。  。  。  :255.255.255.0 
  默认网关 。  。  。  。  。  。  。  。  。  :192.168.1.1 
  事情是在开发一个android应用程序,并且模拟器具有与机器不同的IP类型。 
  我需要获取机器的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(tag, ex.toString());
        }
        return "";
    }
要获得Android设备的Ipaddress,请使用此代码。
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ip = intToIp(ipAddress);
public String intToIp(int i) {
   return ((i >> 24 ) & 0xFF ) + "." +
               ((i >> 16 ) & 0xFF) + "." +
               ((i >> 8 ) & 0xFF) + "." +
               ( i & 0xFF) ;
}
试试这个链接
http://www.droidnova.com/get-the-ip-address-of-your-device,304.html
你也可以试试这个命令adb shell netcfg
链接地址: http://www.djcxy.com/p/65789.html上一篇: Get the IPaddress of the computer in an Android project using java
下一篇: Not receiving mails with Rails actionmailer and delayed job
