2017-08-17 188 views
0

我想要在連接到wifi網絡時獲取運行我的應用程序的手機上用戶的本地IPv4地址。使用下面的代碼:使用java獲取本地網絡的IP地址

WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); 
        String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress()); 
        hostname = ip; 

我能夠得到的東西接近IPv4地址,但相比於在命令行的IPv4地址的時候,它是不完全一樣的。有沒有更好的方法來解決這個問題?我知道formatIpAddress已被棄用,但直到我找到獲取IPv4地址的方式,我現在並不太擔心這一點。

編輯:

我發現,在手機的WiFi設置了IP地址使用的解決方案時,得到這樣的建議的解決方案的IP地址是什麼我得到。有什麼辦法可以在ip config客戶端獲取IP地址嗎?

+4

*我能夠得到接近IPv4地址的東西,但它不是確切的。*這甚至意味着什麼? – shmosel

+0

檢查這個問題:https://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device-from-code –

+0

[此鏈接](https://stackoverflow.com/ questions/40912417/java-getting-ipv4-address)可以幫助你獲得IPv4地址。希望能幫助到你。 – Greenkiller

回答

0

下面的代碼遍歷所有它的接口,然後打印的IPv4,IPv6和接口的MAC地址。對於局域網IP地址,您可以使用功能 isSiteLocal()如果IP地址是本地地址,則返回true。

import java.net.InetAddress; 
import java.net.NetworkInterface; 
import java.net.SocketException; 
import java.net.UnknownHostException; 
import java.util.Enumeration; 
public class App{ 

public static void main(String[] args)throws Exception { 
    // getting the list of interfaces in the local machine 
    Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces(); 
    while(n.hasMoreElements()){ //for each interface 
     System.out.println("----------------------------------------------------"); 
     NetworkInterface e = n.nextElement(); 
    //name of the interface 
     System.out.println("Interface Name: " + e.getName()); 
    /* A interface may be binded to many IP addresses like IPv4 and IPv6 
     hence getting the Enumeration of list of IP addresses */ 
     Enumeration<InetAddress> a = e.getInetAddresses(); 
     while(a.hasMoreElements()){ 
      InetAddress addr = a.nextElement(); 
      String add = addr.getHostAddress().toString(); 
      if(add.length() < 17) 
       System.out.println("IPv4 Address: " + add); 
      else 
       System.out.println("IPv6 Address: " + add); 
     } 
     if(e.getHardwareAddress() != null){ 
        // getting the mac address of the particular network interface 
      byte[] mac = e.getHardwareAddress(); 
        // properly formatting the mac address 
      StringBuilder macAddress = new StringBuilder(); 
      for(int i =0; i < mac.length; i++){ 
       macAddress.append(String.format("%03X%s", mac[i],(i < mac.length -1) ? "-":"")); 
      } 
      System.out.println("Hardware adrress: " + macAddress.toString()); 
     } 
     System.out.println("----------------------------------------------------"); 
    } 
} 

}

在卡利的linux 2.0代碼的輸出是:
------------------------- ---------------------------
接口名稱爲wlan0
的IPv6地址:FE80:0:0:0:1992:d9bc: 7d8c:d85b%wlan0
IPv4地址:192.168.1.137
硬件地址:078-0E4-000-0E7-0B0-046
------------------- ---------------------------------
------------------------------------------------ ----
接口名稱:LO
IPv6地址的:0:0:0:0:0:0:0:1%LO
IPv4地址:127.0.0.1
-------- --------------------------------------------

0

的碼打印在烏爾任的Android的java應用程序正在運行該裝置的本地IPv4地址

try { 
     Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); // gets All networkInterfaces of your device 
     while (networkInterfaces.hasMoreElements()) { 
      NetworkInterface inet = (NetworkInterface) networkInterfaces.nextElement(); 
      Enumeration address = inet.getInetAddresses(); 
      while (address.hasMoreElements()) { 
       InetAddress inetAddress = (InetAddress) address.nextElement(); 
       if (inetAddress.isSiteLocalAddress()) { 
        System.out.println("Your ip: " + inetAddress.getHostAddress()); /// gives ip address of your device 
       } 
      } 
     } 
    } catch (Exception e) { 
     // Handle Exception 
    }