Sunday 15 August 2010

java - Getting My LAN ip address (192.168.xxxx) (IPV4) -


In my Android device I am trying to find my IP address (IPv4).
If I do the following code

  InetAddress inet = InetAddress.getLocalHost (); Println (inet.getHostAddress ()); // giving me 127.0.0.1   

The code is giving me 127.0.0.1.
I have to get the actual IP 198.168.xx.xx.

(The same code in my pc is giving me the real IP though.)

  public static string GetIpAddress (try {enumeration en = NetworkInterface.getNetworkInterfaces (); en.hasMoreElements ();) {NetworkInterface intf = en.nextElement (); (Count enumIpAddr = intf.getInetAddresses (); enumIpAddr.hasMoreElements ();) {InetAddress inetAddress = enumIpAddr.nextElement (); If (InetAddress.isLoopbackAddress () and InetAddress example of Inet4Address) {string ipAddress = inetAddress.getHostAddress (). ToString (); Logs. E ("IP address", "" + IPADress); Return ipadress; }}}} Hold (pre-socket expansion) {Log.e ("socket exception in utilities getip address", ex.toString ()); } Return tap; }   

Allow

Also add to the mainfost.

  & lt; Use-permission Android: name = "android.permission.INTERNET" /> & Lt; Usage-permission Android: name = "android.permission.ACCESS_NETWORK_STATE" />    

No comments:

Post a Comment