networking - Android device returns invalid IP address -


hello using code below ip address of android device,

private string returnipadrress()         {             string ipaddress = null;             try                 {                     (enumeration<networkinterface> en = networkinterface.getnetworkinterfaces(); en.hasmoreelements();)                         {                             networkinterface intf = en.nextelement();                             (enumeration<inetaddress> enumipaddr = intf.getinetaddresses(); enumipaddr.hasmoreelements();)                                 {                                     inetaddress inetaddress = enumipaddr.nextelement();                                     if (!inetaddress.isloopbackaddress())                                         {                                             ipaddress = inetaddress.gethostaddress().tostring();                                         }                                 }                         }                  }             catch (socketexception ex)                 {                     log.e("serveractivity", ex.tostring());                     return null;                 }             return ipaddress;         } 

when test on galaxy tablet(os=2.3) works fine , gives me valid ip address.

i have test on emulator(os=2.2) , gives me ip address 10.0.2.15 valid guess.

but when run on micromax canvas(os=4.1) gives me ip address fe80::d0b3:3fff:fe9d:f68c%p2p0 wrong.

is because of different os version?

how can solve this?

try method:

public static string getipaddress() {     try {         list<networkinterface> interfaces = collections.list(networkinterface.getnetworkinterfaces());         (networkinterface intf : interfaces) {             list<inetaddress> addrs = collections.list(intf.getinetaddresses());             (inetaddress addr : addrs) {                 if (!addr.isloopbackaddress()) {                     string saddr = addr.gethostaddress().touppercase();                     boolean isipv4 = inetaddressutils.isipv4address(saddr);                     if (isipv4 && intf.getdisplayname().startswith("wlan")) {                         return saddr;                     }                 }             }         }     } catch (exception ex) {         return null;     }     return null; } 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -