0

In my app I need to fetch the server ip.. my code to fetch ip is

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();
                Log.v("","ip1--:" + inetAddress);
                Log.v("", "ip2--:" + inetAddress.getHostAddress());       
                String ipv4;
      if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4 = inetAddress.getHostAddress())) {

                    String ip = inetAddress.getHostAddress().toString();
                    Toast.makeText(getApplicationContext(), inetAddress., Toast.LENGTH_SHORT).show();
                    Log.v("","ip---::" + ip);
                    // return inetAddress.getHostAddress().toString();
                    return ipv4;
                }
            }
        }

but it is returning the local address if a device is connected to a networked wifi device. Please do tell how I can fetch the parent ip of the network. Thanks in advance...

Aashutosh Sharma
  • 1,483
  • 2
  • 17
  • 29

2 Answers2

1
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();

Notice that the ipAddress is an integer.

For instance if your IP address is "86.52.119.245":

the int returned by getIpAddress will be 4118230102.
Translated into binary this number is: 11110101 01110111 00110100 01010110.
Convert each byte into a decimal, and then you get the numbers: 80 35 83 10
Notice that the numbers are in network order so you have to flip it.
Aditya Nikhade
  • 1,373
  • 10
  • 15
  • here the function getIpAddress() returns me a 9 digit integer but you have written 10 digit integer number. plz tell the solution.. thnx.. – Aashutosh Sharma Sep 10 '12 at 11:52
  • go ahead and translate it into binary! and then each byte to decimal! if still a prob type the 9 digit number here! – Aditya Nikhade Sep 10 '12 at 12:00
  • I have a binary string of 30 digits plz tell me the method to convert it to ip address. you have make the groups of 8 but in my case there are 2 bits are less. Now what to do?? Please help.. – Aashutosh Sharma Sep 10 '12 at 12:03
  • I have tried your provided integer 4118230102 in the function Integer.toBinaryString(4118230102); but it is giving error of out of range integer. I am totally stuck here pls help me out pls... – Aashutosh Sharma Sep 10 '12 at 12:06
  • Thanks Aditya for your precious answers my problem has solved I have refered to this link http://stackoverflow.com/questions/7975473/detect-wifi-ip-address-on-android – Aashutosh Sharma Sep 10 '12 at 12:24
  • your always welcome.. you can close close qestion by clicking the answer right! :) – Aditya Nikhade Sep 10 '12 at 12:25
  • No my answer has not closed because it gives me my local IP address rather than the server IP... I want to fetch the server IP from the network... how I can do it pls do tell.. – Aashutosh Sharma Sep 10 '12 at 12:32
  • check my new answer hope this will help! – Aditya Nikhade Sep 10 '12 at 12:35
1
private String wifi_ip() 
    {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet("http://wiki.iti-lab.org/ip.php");
            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);
                    ip1 = str;
                }
            }
        } catch (Exception e) {
            // ip.setText("Error");
        }
        return ip1;
    }
Aditya Nikhade
  • 1,373
  • 10
  • 15