0

I am trying to read devices Ip address which has given from isp's not local.

How to get IP address of the device from code? In this checked answer gives me LAN Ip address for WI-FI and gives different IP address than whatismyip on the cell internet. I guess it may be local ip of operators service provider.

How can I internet Ip address what returns whatismyip from?

public static String getPublicIP() throws IOException
{
    Document doc = Jsoup.connect("http://www.checkip.org").get();
    return doc.getElementById("yourip").select("h1").first().select("span").text();
}

This one may work but I do not want to add any library just for it.

smallBig Edit: http://api.exip.org/?call=ip it returns just ip, how can I use it? And is it reliable and long life?

Community
  • 1
  • 1
John simit
  • 1,305
  • 2
  • 11
  • 14
  • If you are behind a proxy, services might give you that IP when you are on cell. Depending on what you are going to use it for the actual IP whatismyip returns is _NOT_ what you are looking for. – Nanne May 31 '13 at 08:17
  • @Nanne, I do not use any proxy, I want it for ftp and remote controlling. I guess, what returns wahtismyip is the what I looking for as ISP or Cell WAN IP. – John simit May 31 '13 at 08:39
  • 1
    But it could be your ISP uses some sort of proxy. If your sell has a different IP then whatever whatismyip returns, then you will have trouble connecting -> if you connect to the 'whatismyip' address, it will not know to send the request to your phone, just like with a local LAN ip and your WAN ip at home (wifi). So what you need is the local ip from your device. If that is different then the 'whatismyip' address, you are behind some sort of more complicated thing (NAT?) and you won't be helped much by retrieving the other address – Nanne May 31 '13 at 09:26

2 Answers2

0

If you don't want to add a library, use URLConnection and do the parsing yourself.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
-1
public void getCurrentIP () {
  ip.setText("Please wait...");  
  try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://whatismyip.everdot.org/ip");
        // HttpGet httpget = new HttpGet("http://whatismyip.com.au/");
        // HttpGet httpget = new HttpGet("http://www.whatismyip.org/");
        HttpResponse response;

        response = httpclient.execute(httpget);

        //Log.i("externalip",response.getStatusLine().toString());

        HttpEntity entity = response.getEntity();
        if (entity != null) {
                long len = entity.getContentLength();
                if (len != -1 && len < 1024) {
                        String str=EntityUtils.toString(entity);
                        //Log.i("externalip",str);
                        ip.setText(str);
                } else {
                        ip.setText("Response too long or error.");
                        //debug
                        //ip.setText("Response too long or error: "+EntityUtils.toString(entity));
                        //Log.i("externalip",EntityUtils.toString(entity));
                }            
        } else {
                ip.setText("Null:"+response.getStatusLine().toString());
        }

  }
  catch (Exception e)
  {
      ip.setText("Error");
  }

}
Jonathan Kortleven
  • 613
  • 1
  • 5
  • 16
  • It returns an errors as `android.os.NetworkOnMainThreadException` :( – John simit May 31 '13 at 08:41
  • 1
    From JellyBean onwards, any code for network access has to be run in a separate thread. i.e. other than the main / ui thread – midhunhk May 31 '13 at 08:51
  • 1
    @IAMTHEANSWER Had the code in one of my applications, didn't know where it came from. Sorry :) – Jonathan Kortleven May 31 '13 at 09:24
  • 2
    @JonathanKortleven Please don't feel compelled to apologize to a rude user. I do encourage you to always consider *constructive* feedback - but calling you a 'bloody fool' was not constructive. – Andrew Barber May 31 '13 at 12:13