2

i am trying to get the Internet Ip Address in my mobile. no need for device ip address. that device is connected with wifi or 3g or 2g or anyway. that mobile is having internet facility means, i need that internet IP Address.

Note : already i tried some coding. that code will work to get the device ip address only.

my prev code :

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("ServerActivity", ex.toString());
  }

Another one code :

 WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ipAddress = wifiInfo.getIpAddress();
    Log.v("hari", "ipAddress:"+ipAddress);

Thanks Advance please any one help me..

harikrishnan
  • 1,985
  • 4
  • 32
  • 63

1 Answers1

4

You can use this website and their API to get it: http://www.externalip.net/

Specifically http://api.externalip.net/ip

Alternatively (as mentioned by someone in the comments) you could create a PHP file on your server:

<?php
echo $_SERVER['REMOTE_ADDR'];
?>

Android code using the externalip.net url (adapted from this answer)

HttpGet httppost = new HttpGet("http://api.externalip.net/ip");
HttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
String ipaddress = r.readLine();
Community
  • 1
  • 1
LondonAppDev
  • 8,501
  • 8
  • 60
  • 87
  • now am using this method only..but, today morning onwards, i git error while using this method..thats why asking another method.. – harikrishnan Sep 20 '13 at 13:42
  • Or you can write your own and host it on your own server quite easily. It's a one-liner in php. – 323go Sep 20 '13 at 13:42
  • i need it for android front end side internet ip detecting – harikrishnan Sep 20 '13 at 13:47
  • @harikrishnan what do you mean? I put the android code, is this what you are looking for? – LondonAppDev Sep 20 '13 at 13:53
  • i am using this same only.. this is working for correctly..but, today morning to afternoon till. this method show error.. – harikrishnan Sep 20 '13 at 13:55
  • Sir, is it working now? Maybe earlier this morning the internet was disconnected or the externalip.net website was down? What was the error you got this morning exactly? – LondonAppDev Sep 20 '13 at 13:58
  • but, its not good one to depend another website. if anyother way is there to find ip means, pls tell me.. thanks.. – harikrishnan Sep 20 '13 at 14:01
  • Do you have access to a server running Apache and PHP? In my answer above I put the code for a simple PHP file which will return the IP address of the client. You could simply create something like getip.php and paste my code in there, and then go to http://yourwebservice.com/getip.php instead of externalip.net. – LondonAppDev Sep 20 '13 at 14:03