我如何获得IP_ADDRESS IPv4格式 [英] How do I get IP_ADDRESS in IPV4 format

查看:268
本文介绍了我如何获得IP_ADDRESS IPv4格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个设备的IP地址,即使用WIFI或3G连接。我收到的IPv6格式是无法理解的ip地址。我想IPv4格式的IP address.I做了谷歌,但力所发现的任何适当的解决办法。

I am trying to get the IP address of an device i.e using WIFI or 3G connection. I am getting the ip address in IPV6 format which is not understandable. I want in IPV4 format IP address.I have done google but dint found any proper solutions.

下面是我用来获得设备的IP地址code

here is code which I am using to get IP address of an device

public String getLocalIpAddress() {
    try {
        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();
                System.out.println("ip1--:" + inetAddress);
                System.out.println("ip2--:" + inetAddress.getHostAddress());
                if (!inetAddress.isLoopbackAddress()) {


                    String ip = inetAddress.getHostAddress().toString();
                    System.out.println("ip---::" + ip);
                    EditText tv = (EditText) findViewById(R.id.ipadd);
                    tv.setText(ip);
                    return inetAddress.getHostAddress().toString();

                }
            }
        }
    } catch (Exception ex) {
        Log.e("IP Address", ex.toString());
    }
    return null;
}

我收到此输出中:

I am getting this ouput :

ip1--:/fe80::5054:ff:fe12:3456%eth0%2
ip2--:fe80::5054:ff:fe12:3456%eth0

应该是这样显示的:

It should be displayed like this :

192.168.1.1

请帮我..

推荐答案

在尝试很多窍门。最后我能为IPv4格式的IP地址。这里是我的code ..

After trying many tricks.. finally I can get the IP address in IPV4 format.. Here is my code..

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();
                System.out.println("ip1--:" + inetAddress);
                System.out.println("ip2--:" + inetAddress.getHostAddress());

      // for getting IPV4 format
      if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())) {

                    String ip = inetAddress.getHostAddress().toString();
                    System.out.println("ip---::" + ip);
                    EditText tv = (EditText) findViewById(R.id.ipadd);
                    tv.setText(ip);
                    // return inetAddress.getHostAddress().toString();
                    return ipv4;
                }
            }
        }
    } catch (Exception ex) {
        Log.e("IP Address", ex.toString());
    }
    return null;
}

新增如果,如下图所示。

 /**This shows IPV4 format IP address*/
 if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())){}

而不是这个

 /**This shows IPV6 format IP address*/
 if (!inetAddress.isLoopbackAddress()){}

非常感谢.. 拉胡尔

Many Thanks.. Rahul

有检查所述地址是版本4地址另一种方法是:

An alternative for checking if the address is a version 4 address is:

if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address)

这篇关于我如何获得IP_ADDRESS IPv4格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆