获取IP地址

获取IP地址:

public class GetLocalIp {

    /**
     * 获取IP地址
     *
     * @return
     * @throws SocketException
     */
    public static String getLocalIPAddress() throws SocketException {
        for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
        return "null";
    }
}

 

你可能感兴趣的:(【Android常用代码块】,获取IP地址)