2012-06-13 33 views
20

WIFI veya 3G bağlantısını kullanarak bir aygıtın IP adresini almaya çalışıyorum. IP adresi IPV6 biçiminde anlaşılamıyor. IPv4 formatında IP adresi istiyorum.Google'a yaptım ama dint uygun herhangi bir çözüm buldu. BuradaIP_ADDRESS IPV4 biçiminde nasıl edinilir

Bir cihazın IP adresini almak için kullanıyorum kodudur

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

bu çıktıya alıyorum:

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

Böyle görüntülenmesi gereken:

192.168.1.1 

lütfen bana yardım edin ..

cevap

44

birçok hileler çalışıyor .. nihayet IPV4 formatında IP adresi alabilirsiniz sonra .. İşte benim kodudur ..

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

Eklendi eğer

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

aşağıda gösterildiği gibi koşul bunun yerine bu

Çok teşekkürler .. Rahul

adresi sürüm 4 adres olup olmadığını kontrol için alternatif şudur: herhangi bir cihaz sadece bir ağ adresine sahip olduğunu kabul edemeyiz

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

Null sonucunu önlemek için izin vermeyin : Vyacheslav

+0

"ipv4" olarak nerede ve nasıl bildirilir – CrazyMind

2

IPv4 adresleri için Java API'sinde ayrı bir sınıf Inet4Address var gibi görünüyor.

+0

Denemeyi denedim ama bir ipucunun varsayılan ip adresi localhost ip adresi değil .. –

+0

Localhost 127.0.0.1, ne istediğinizi şüpheliyim. –

+0

@ Tech.Rahul Eğer localhost almak istediyseniz InetAddress addr = InetAddress.getLocalHost(); '? – 0nyx

8

. Ayrıca, herhangi bir IPv4'e sahip olduğunu da kabul edemezsiniz - sadece IPv6 olabilir, bu nedenle uygulamanızın hem IPv4 hem de IPv6 adres ekranlarını ele alması gerekir. Tipik olarak, bir Android telefonun, kullanılabilir IP adresleri, 3G verisi için rmnet0 atanan en az iki arabirimi vardır; bu, IPv4 için genellikle taşıyıcı sınıf NAT'tır ve böylece gelen soket bağlantılarını kabul edemez ve ayrıca bir IPv6 olabilir. adres; IPv4 ve/veya IPv6 adresi ne olursa olsun alacağı ağ ile müzakere edebilecektir wifi için wlan0. 3G veri kullanımını azaltmak amacıyla Android'in bazı sürümleri, kablosuz olarak bağlandığında (genellikle daha pahalı) rmnet0 bağlantısını bilerek bırakacaktır. Bu davranış, wifi manuel bir giriş gerektiren bir esir portalı olan bir şeye bağlı olduğunda bir sorundur.