2012-06-13 31 views
20

我正在嘗試獲取設備的IP地址,即使用WIFI或3G連接。我得到IPV6格式的IP地址,這是不可理解的。我想IPV4格式的IP地址。我已經完成谷歌,但力德發現任何適當的解決方案。如何獲得IPV4格式的IP_ADDRESS

這裏是我使用得到一個設備的IP地址碼

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; 
} 

我得到這個輸出中:

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

應該顯示如下:

192.168.1.1 

請幫我解決..

回答

44

後試圖花樣繁多。最後,我可以得到IPv4格式的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(); 
       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 ip; 
       } 
      } 
     } 
    } 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()){} 

非常感謝.. 拉胡爾

檢查,如果地址是版本4的地址的替代方法是:

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

不要忘記權限,以防止空結果: Vyacheslav

+0

聲明「ipv4」的地方和聲明 – CrazyMind

2

似乎在用於IPv4地址的Java API中有一個單獨的類Inet4Address

+0

我試過,但它給出了一個模擬器的默認IP地址不是本地主機的IP地址.. –

+0

本地主機是127.0.0.1,我懷疑這是你想要的。 –

+0

@ Tech.Rahul如果你想得到本地主機,你可以不使用像'InetAddress addr = InetAddress.getLocalHost();'? – 0nyx

8

你不能假設任何設備只有一個網絡地址。您也不能認爲它將具有任何IPv4 - 它可能僅限於IPv6,因此您的應用程序需要能夠處理IPv4和IPv6地址顯示。

通常情況下,Android手機至少有兩個接口分配可用的IP地址rmnet0用於3G數據,這對於IPv4通常是運營商級NAT,因此不能接受傳入的套接字連接,並且也可能具有IPv6地址; wlan0爲wifi,它將擁有任何IPv4和/或IPv6地址,可以與它所連接的網絡進行協商。

某些版本的Android會在連接到wifi時故意刪除(通常較昂貴的)rmnet0鏈接 - 以嘗試減少3G數據使用量。當wifi連接到需要手動登錄的強制門戶網站時,此行爲就成了問題。