0

I have created an Android app which uses WebView to load internet pages. I want to get the mobile user IP Address. How to do it??

user2129794
  • 2,388
  • 8
  • 33
  • 51

2 Answers2

0

This will load ip address on screen

 webview.loadUrl("http://www.whatismyip.com")
DjHacktorReborn
  • 2,908
  • 2
  • 20
  • 29
-1
public static String getipAddress() { 
            try {
                for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                    NetworkInterface intf = en.nextElement();
                    for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        if (!inetAddress.isLoopbackAddress()) {
                            String ipaddress=inetAddress.getHostAddress().toString();
                            Log.e("ip address",""+ipaddress);
                            return ipaddress;
                        }
                    }
                }
            } catch (SocketException ex) {
                Log.e("Socket exception in GetIP Address of Utilities", ex.toString());
            }
            return null; 
    }

Also add in mainfest

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
QuokMoon
  • 4,387
  • 4
  • 26
  • 50
  • Am getting casting error at two places: `NetworkInterface intf = **(NetworkInterface)** en.nextElement();` `InetAddress inetAddress = **(InetAddress)** enumIpAddr.nextElement();` – user2129794 Mar 08 '13 at 18:43