当提供移动连接时,我尝试使用以下代码获取internet动态IP地址。getHostAddress返回10.13.x.xSSID内部主机地址。但是我想把作为178.240.x.x动态互联网IP地址。提前谢谢。
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
networkIpAdress = inetAddress.getHostAddress().toString();
}
}发布于 2019-01-24 05:46:40
使用此URL获取公共IP地址:
https://ident.me注意:您可能同时拥有IPv4和IPv6地址,在这种情况下,您可以分别使用https://v4.ident.me和https://v6.ident.me。
文档位于https://api.ident.me。
发布于 2014-10-27 09:32:19
如果您的设备与wifi连接,那么
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();安卓设备既可以通过WiFi连接,也可以通过3G连接,3G显然有不同的IP。3G上的IP地址也将在每次重新连接时发生变化。
发布于 2014-10-27 14:55:32
我用以下代码解决了我的问题。
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
HttpResponse response;
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
entity.getContentLength();
JSONObject json_data = new JSONObject(EntityUtils.toString(entity));
String networkIpAdress = json_data.getString("ip");https://stackoverflow.com/questions/26584380
复制相似问题