2

--> IP address of Android Application end user

I have searched allot over internet about how to capture the IP ADDRESS of user who is actually using my ANDROID APPLICATION, but did not get the correct code which will work for me.

I am developing this android app on Eclipse.

on most of the forums i get this link which is not reachable at all: http://www.droidnova.com/get-the-ip-address-of-your-device,304.html

Need your support guys.

Thanks

  • 1
    Is this what you want ? http://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device Utils.getIPAddress(true); // IPv4 Utils.getIPAddress(false); // IPv6 – shemsu May 07 '13 at 12:28

3 Answers3

3

You can try the following code.

  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();
           if (!inetAddress.isLoopbackAddress()) {
             String ip = Formatter.formatIpAddress(inetAddress.hashCode());
             return ip;
           }
         }
      }
    } catch (SocketException ex) {
      // 
    }
    return null;
  }

Don't forget to add the following permission in order to allow your application to open network sockets:

 <uses-permission android:name="android.permission.INTERNET" /> 
Naskov
  • 4,121
  • 5
  • 38
  • 62
Igo Coelho
  • 47
  • 5
  • @Ankur - I am getting this error "The method formatIpAddress(int) from the type Formatter is deprecated" on this line "getting String ip = Formatter.formatIpAddress(inetAddress.hashCode());" – ChitranjanThakur May 07 '13 at 12:41
  • Switch the Formatter line CT talked about to: String ip = inetAddress.getHostAddress(); to avoid the deprecation warning – Chris Klingler Mar 13 '15 at 22:49
1
  1. try to open a connection to http://www.whatismyip.com and fetch the result.
  2. Make a HTTP POST ( or GET) to your app host and check there from which IP is coming the request.
  3. use SDK.

Edit: based on comment: that is the server side role to get the client IP, not the Android job. Here is a sample how can you do it with PHP: $_SERVER['REMOTE_ADDR']

0

Or instead of requesting the IP from the Server, you can tell your phone to send the IP.

You can get the IP adress with the following code.

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();
                    if (!inetAddress.isLoopbackAddress()) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e(tag, ex.toString());
        }
        return "";
    }
Naskov
  • 4,121
  • 5
  • 38
  • 62