/** * @author YOLANDA * @Time 2015年2月27日 上午10:04:50 */public class InetUtil { /*这里要加上一点说明,因为在21开始,HTTPClient被抛弃了 * Google推荐使用URLConnect,这里的Ipv4也被抛弃了, * 为了兼容以后的版本,我把HTTPClient的一些源码直接拿到项目中来用 * 所以这里出现了Ipv4的检查的源码*/ /** * 获取本地IP地址 * @author YOLANDA * @return */ public static String getLocalIPAddress() { String ipAddress = ""; try { Enumeration<NetworkInterface> netfaces = NetworkInterface.getNetworkInterfaces(); // 遍历所用的网络接口 while (netfaces.hasMoreElements()) { NetworkInterface nif = netfaces.nextElement();// 得到每一个网络接口绑定的地址 Enumeration<InetAddress> inetAddresses = nif.getInetAddresses(); // 遍历每一个接口绑定的所有ip while (inetAddresses.hasMoreElements()) { InetAddress ip = inetAddresses.nextElement(); if (!ip.isLoopbackAddress() && isIPv4Address(ip.getHostAddress())) { ipAddress = ip.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } return ipAddress; } /** * Ipv4地址检查 */ private static final Pattern IPV4_PATTERN = Pattern.compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"); /** * 检查是否是有效的IPV4地址 * @param input the address string to check for validity * @return true if the input parameter is a valid IPv4 address */ public static boolean isIPv4Address(final String input) { return IPV4_PATTERN.matcher(input).matches(); } /*===========以下是IPv6的检查,暂时用不到==========*/ /*//未压缩过的IPv6地址检查 private static final Pattern IPV6_STD_PATTERN = Pattern.compile("^[0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}$"); //检查参数是否有效的标准(未压缩的)IPv6地址 public static boolean isIPv6StdAddress(final String input) { return IPV6_STD_PATTERN.matcher(input).matches(); } //压缩过的IPv6地址检查 private static final Pattern IPV6_HEX_COMPRESSED_PATTERN = Pattern.compile( "^(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)" + // 0-6 hex fields "::" + "(([0-9A-Fa-f]{1,4}(:[0-9A-Fa-f]{1,4}){0,5})?)$"); // 0-6 hex fields //检查参数是否有效压缩IPv6地址 public static boolean isIPv6HexCompressedAddress(final String input) { int colonCount = 0; for(int i = 0; i < input.length(); i++) { if (input.charAt(i) == ':') { colonCount++; } } return colonCount <= 7 && IPV6_HEX_COMPRESSED_PATTERN.matcher(input).matches(); } //检查是否是压缩或者未压缩过的IPV6地址 public static boolean isIPv6Address(final String input) { return isIPv6StdAddress(input) || isIPv6HexCompressedAddress(input); }*/} 详细解决方案
Android获取本地IP地址,Ipv4地址检察,Ipv6地址检查
热度:73 发布时间:2016-04-28 01:42:02.0
相关解决方案
- android 读取byte[]中的元素解决方案
- android 标题栏兑现方式
- android 中Activity向BroadcastReceiver发送数据,该怎么解决
- Android 4.0 为什么模拟器老是提示小弟我谷歌拼音输入法已停止
- android:getSharedPreferences() 这是哪个类的方法解决思路
- android 怎么判断一个程序是否联网
- android RadioButton如何设置默认选中
- android 怎么重新设置锚点
- android 图片对象获取的有关问题
- android 关于服务连接的疑义
- android 怎么实现对view的放大和缩小
- android ID,该如何处理
- 准备复习2-3个月,看java+android,请问有经验者,怎么看效果最好》
- android UI线程与AsyncTask的有关问题
- android(java) 中文乱码的有关问题
- android 动态设立控件高度
- Android test project 编译方法
- android 4.03启动出错,显示"Encryption Unsuccessful"该如何解决
- Android 下面的listView的动态效果怎么实现的?求源代码,多谢
- Android?Palm?Symbian?Windows Mobile?学哪个有“钱图”?解决方案
- Android 开发语言,该怎么解决
- 单个人去做手机开发选什么平台好?android,iphone?解决办法
- Android 虚拟机崩溃的有关问题
- 想学学移动平台的开发 android 还是windows phone7好啊该怎么处理
- Android 上的如何读写设备文件
- Android 2.2 API 汉语文档系列(3) —— AccessibilityService
- 【先睹为快】ArcGIS Android SDK上一版本新特性
- ArcGIS for Android app v2.0.4公布
- android.hardware.USB种介绍
- android linux开发 在线电子书解决方案