title: How to get the device public ip address in android and java
body: <p>How to get the device public IP address in android.</p>
  
  <p>please find my code here. i am getting device ip address. but how i can get the public ip address.</p>
  
  <p>Please help me </p>
  
  <pre><code>import java.util.Enumeration;
  
  public class GetDeviceIp
  {
    public GetDeviceIp() {}
  
    public static String getIpAddress()
    {
      try {
        Enumeration&lt;java.net.NetworkInterface&gt; en = 
          java.net.NetworkInterface.getNetworkInterfaces(); 
          Enumeration&lt;java.net.InetAddress&gt; enumIpAddr;
          for (; en.hasMoreElements(); enumIpAddr.hasMoreElements())
        {
          java.net.NetworkInterface intf = (java.net.NetworkInterface)en.nextElement();
          enumIpAddr = intf.getInetAddresses();continue;
          java.net.InetAddress inetAddress = (java.net.InetAddress)enumIpAddr.nextElement();
          if ((!inetAddress.isLoopbackAddress()) &amp;&amp; 
            ((inetAddress instanceof java.net.Inet4Address))) {
            return inetAddress.getHostAddress();
          }
        }
      }
      catch (java.net.SocketException ex) {
        ex.printStackTrace();
        return null;
      }
  
    }
  }
  </code></pre>