Java org.apache.http.conn.util.InetAddressUtils 代码实例

・22 分钟阅读

以下是展示如何使用org.apache.http.conn.util.InetAddressUtils的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


/**
 * Get IP address from first non-localhost interface
 *
 * @param useIPv4 true=return ipv4 address, false=return ipv6 address
 * @return address or empty string
 */
protected static String getIPAddress(boolean useIPv4)
{
   try {
      List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
      for (NetworkInterface intf : interfaces) {
         List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
         for (InetAddress addr : addrs) {
            if (!addr.isLoopbackAddress()) {
               String sAddr = addr.getHostAddress().toUpperCase();
               boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
               if (useIPv4) {
                  if (isIPv4) {
                     return sAddr;
                  }
               }
               else {
                  if (!isIPv4) {
                     int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                     return delim < 0 ? sAddr : sAddr.substring(0, delim);
                  }
               }
            }
         }
      }
   }
   catch (Exception ex) {} // ignore exceptions
   return "";
}
 

实例 2


/**
 * Get IP address from first non-localhost interface
 * @param ipv4  true=return ipv4, false=return ipv6
 * @return  address or empty string
 */
public static String getIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); 
                    if (useIPv4) {
                        if (isIPv4) 
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim<0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) { } // for now eat exceptions
    return "";
}
 

实例 3


/**
 * Returns the first non-local IPv4 address of the device. 
 * @return IPv4 address as String or unknown, if no address is found.
 */
private static String getLocalIpAddress() {
    try {
        for(NetworkInterface iface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
                for (InetAddress address : Collections.list(iface.getInetAddresses())) {
                        if (!address.isLoopbackAddress() && address.isSiteLocalAddress() && InetAddressUtils.isIPv4Address(address.getHostAddress())) {
                                return address.getHostAddress().toString();
                        }
                }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return "unknown";
}
 

实例 4


/**
 * Get IP address from first non-localhost interface
 *
 * @param useIPv4 true=return ipv4, false=return ipv6
 * @return address or empty string
 */
public static String getIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (intf.getName().contains("wlan")) {
                List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
                for (InetAddress addr : addrs) {
                    if (!addr.isLoopbackAddress()) {
                        String sAddr = addr.getHostAddress().toUpperCase();
                        boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                        if (useIPv4) {
                            if (isIPv4)
                                return sAddr;
                        } else {
                            if (!isIPv4) {
                                int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                                return delim < 0 ? sAddr : sAddr.substring(0, delim);
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
}
 

实例 5


public String getLocalIpAddress() {
    String ip6 = null;
    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()) {
                    String address = inetAddress.getHostAddress();
                    if (InetAddressUtils.isIPv4Address(address)) { //Prefer IPv4 over IPv6, because sshfs doesn't seem to like IPv6
                        return address;
                    } else {
                        ip6 = address;
                    }
                }
            }
        }
    } catch (SocketException ex) {
    }
    return ip6;
}
 

实例 6


public static String getIPAddress(boolean useIPv4) {
        try {
                List<NetworkInterface> interfaces = Collections
                                .list(NetworkInterface.getNetworkInterfaces());
                for (NetworkInterface intf : interfaces) {
                        List<InetAddress> addrs = Collections.list(intf
                                        .getInetAddresses());
                        for (InetAddress addr : addrs) {
                                if (!addr.isLoopbackAddress()) {
                                        String sAddr = addr.getHostAddress().toUpperCase();
                                        boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                                        if (useIPv4) {
                                                if (isIPv4)
                                                        return sAddr;
                                        } else {
                                                if (!isIPv4) {
                                                        int delim = sAddr.indexOf('%'); // drop ip6 port
                                                        // suffix
                                                        return delim < 0 ? sAddr : sAddr.substring(0,
                                                                        delim);
                                                }
                                        }
                                }
                        }
                }
        } catch (Exception e) {
                e.printStackTrace();
        }
        return "";
}
 

实例 7


public static String getIPAddress() {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                       if (InetAddressUtils.isIPv4Address(sAddr)) {
                            return sAddr;
                       }
                }
            }
        }
    } catch (Exception ex) { }
    return "";
}
 

实例 8


private void getLocalIpAddress(boolean useIPv4) {
        try {
                List<NetworkInterface> interfaces = Collections
                                .list(NetworkInterface.getNetworkInterfaces());
                for (NetworkInterface intf : interfaces) {
                        List<InetAddress> addrs = Collections.list(intf
                                        .getInetAddresses());
                        for (InetAddress addr : addrs) {
                                if (!addr.isLoopbackAddress()) {
                                        String sAddr = addr.getHostAddress().toUpperCase();
                                        boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                                        if (useIPv4) {
                                                if (isIPv4) {
                                                        // Log.i("IP", "***** IP=" + sAddr);
                                                        heurData.setHeuristicData("IP", sAddr);
                                                        ;
                                                }
                                        } else {
                                                if (!isIPv4) {
                                                        int delim = sAddr.indexOf('%'); // drop ip6 port
                                                                                                                        // suffix
                                                        heurData.setHeuristicData(
                                                                        "IP",
                                                                        delim < 0 ? sAddr : sAddr.substring(0,
                                                                                        delim));
                                                }
                                        }
                                }
                        }
                }
        } catch (Exception ex) {
        } // for now eat exceptions
        heurData.setHeuristicData("IP", "Undefined");
}
 

实例 9


/**
 * Get IP address from first non-localhost interface
 * @param ipv4  true=return ipv4, false=return ipv6
 * @return  address or empty string
 */
public static String getIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); 
                    if (useIPv4) {
                        if (isIPv4) 
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim<0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) { } // for now eat exceptions
    return "";
}
 

实例 10


/**
 * Returns the first non-local IPv4 address of the device. 
 * @return IPv4 address as String or unknown, if no address is found.
 */
private String getLocalIpAddress() {
    try {
        for(NetworkInterface iface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
                for (InetAddress address : Collections.list(iface.getInetAddresses())) {
                        if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(address.getHostAddress())) {
                                return address.getHostAddress().toString();
                        }
                }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return dc.getContext().getString(android.R.string.unknownName);
}
 

实例 11


/**
 * This method gets an IPv4 address for the user to enter in the browser.
 * 
 * @return The IP
 */
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() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress()))
                                {
                                        return inetAddress.getHostAddress().toString();
                                }
                        }
                }
        }
        catch(SocketException e)
        {
                e.printStackTrace();
        }
        return null;
}
 

实例 12


private void setDeviceIp() {
        try {
           List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
           for (NetworkInterface intf : interfaces) {
               List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
               for (InetAddress addr : addrs) {
                   if (!addr.isLoopbackAddress()) {
                       String sAddr = addr.getHostAddress().toUpperCase();
                       boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); 
                       if (isIPv4) {
                        deviceIP = sAddr;
                       }
                   }
               }
           }
       } catch (Exception ex) {
        deviceIP = null;
       }
}
 

实例 13


/**
 * Get IP address from first non-localhost interface
 * @param useIPv4  true=return ipv4, false=return ipv6
 * @return  address or empty string
 */
public static String getIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim<0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) { } // for now eat exceptions
    return "";
}
 

实例 14


/** For mobile */
public static String getip4Address() {
        try {
                for (Enumeration<NetworkInterface> en = NetworkInterface
                                .getNetworkInterfaces(); en.hasMoreElements();) {
                        NetworkInterface intf = (NetworkInterface) en.nextElement();
                        for (Enumeration<InetAddress> enumIpAddr = intf
                                        .getInetAddresses(); enumIpAddr.hasMoreElements();) {
                                InetAddress inetAddress = enumIpAddr.nextElement();
                                if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
                                        String ipaddress = inetAddress .getHostAddress()
                                                        .toString();
                                        Log.d("ip address", "" + ipaddress);
                                        return ipaddress;
                                }
                        }
                }
        } catch (SocketException ex) {
                Log.e(TAG, "Exception in Get IP Address: " + ex.toString());
        }
        return null;
}
 

实例 15


private static String getIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port
                            // suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
}
 
讨论
淘淘あ西西 profile image