我想在我的手机上获取互联网IP地址。不需要设备IP地址。该设备连接wifi或3g或2g或无论如何。移动设备有互联网设施,我需要互联网IP地址。
注意:我已经尝试了一些编码。该代码仅用于获取设备IP地址。
我的优先代码:
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();
if (!inetAddress.isLoopbackAddress())
{ return inetAddress.getHostAddress().toString(); }
}
}
}
catch (SocketException ex)
{
Log.e("ServerActivity", ex.toString());
}
Another one code :
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
Log.v("hari", "ipAddress:"+ipAddress);
谢谢你的进步 请任何人帮助我..
答案 0 :(得分:4)
您可以使用此网站及其API来获取它: http://www.externalip.net/
具体地 http://api.externalip.net/ip
或者(如评论中某人所述),您可以在服务器上创建一个PHP文件:
<?php
echo $_SERVER['REMOTE_ADDR'];
?>
使用externalip.net网址的Android代码(改编自this回答)
HttpGet httppost = new HttpGet("http://api.externalip.net/ip");
HttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
StringBuilder total = new StringBuilder();
String ipaddress = r.readLine();