问题
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