How to get User IP address surfing internet on my Android App through his phone/tablet [duplicate]

筅森魡賤 提交于 2019-12-12 02:05:16

问题


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??


回答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" />



回答2:


This will load ip address on screen

 webview.loadUrl("http://www.whatismyip.com")


来源:https://stackoverflow.com/questions/15300692/how-to-get-user-ip-address-surfing-internet-on-my-android-app-through-his-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!