0

How to get the device public IP address in android.

please find my code here. i am getting device ip address. but how i can get the public ip address.

Please help me

import java.util.Enumeration;

public class GetDeviceIp
{
  public GetDeviceIp() {}

  public static String getIpAddress()
  {
    try {
      Enumeration<java.net.NetworkInterface> en = 
        java.net.NetworkInterface.getNetworkInterfaces(); 
        Enumeration<java.net.InetAddress> 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()) && 
          ((inetAddress instanceof java.net.Inet4Address))) {
          return inetAddress.getHostAddress();
        }
      }
    }
    catch (java.net.SocketException ex) {
      ex.printStackTrace();
      return null;
    }

  }
}
Remph
  • 323
  • 1
  • 4
  • 14
GRaj
  • 31
  • 1
  • 2
  • 10

1 Answers1

1

The simplest way is to create http request that will get public IP from server that returns it. I used http://whatismyip.akamai.com/ since it does not require any parsing and probably won't get simpler than that.

As for the code, you need to create another thread which will send http request, it is not possible from main thread. Presented solution lacks timeout and it is poor example of multithreading, it may be implemented.

public static String getPublicIPAddress(){
    String value = null;
    ExecutorService es = Executors.newSingleThreadExecutor();
    Future<String> result = es.submit(new Callable<String>() {
        public String call() throws Exception {
            try {
                URL url = new URL("http://whatismyip.akamai.com/");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                try {
                    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                    BufferedReader r = new BufferedReader(new InputStreamReader(in));
                    StringBuilder total = new StringBuilder();
                    String line;
                    while ((line = r.readLine()) != null) {
                        total.append(line).append('\n');
                    }
                    urlConnection.disconnect();
                    return total.toString();
                }finally {
                    urlConnection.disconnect();
                }
            }catch (IOException e){
                Log.e("Public IP: ",e.getMessage());
            }
            return null;
        }
    });
    try {
        value = result.get();
    } catch (Exception e) {
        // failed
    }
    es.shutdown();
    return value;
}
Myszsoda
  • 339
  • 5
  • 15
  • Thanks, @Myszsoda. now I able to get the public IP address. but I have one question if I use URL url = new URL("http://whatismyip.akamai.com/"); any problem with the "http://whatismyip.akamai.com/" URL . because I deploy the jar file in production. Please provide some information. any licenses are required? – GRaj Oct 12 '18 at 12:49
  • As far as I know, there is no licensing for it as it is just website returning IP. And I highly doubt that you need any licensing for "http request". And by that, you may use any website that returns your IP, although I couldn't find website with such simple response. As said, it is not licensed API, so you should be free to use it, but you may consider option that website won't work. – Myszsoda Oct 12 '18 at 13:22