我想获取我的Android手机所连接的wifi路由器的IP地址?我知道我们可以通过使用android APIS获取mac/BSSId和SSID,但我找不到找到找到它的IP地址的方法吗?
我发现获取手机IP地址的代码拥有wifi路由器
WifiManager myWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
int ipAddress = myWifiInfo.getIpAddress();
System.out.println("WiFi address is " + android.text.format.Formatter.formatIpAddress(ipAddress))
Run Code Online (Sandbox Code Playgroud)
但未能得到我想要的东西
我有找到DHCP地址和主机地址的方法.如何查找分配给我的设备的动态IP地址.我可以从http://www.ip2location.com/default.aspx获取地址
如何在设备中获取此IP?
可能重复:
如何获取设备的IP地址?
下面是我如何尝试获取外部IP的片段.但是,它没有返回任何东西......
public String getIpAddress() {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.whatismyip.com/?404");
// HttpGet httpget = new HttpGet("http://whatismyip.com.au/");
// HttpGet httpget = new HttpGet("http://www.whatismyip.org/");
HttpResponse response;
response = httpclient.execute(httpget);
//Log.i("externalip",response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
entity.getContentLength();
str = EntityUtils.toString(entity);
}
catch (Exception e)
{
}
return str;
}
Run Code Online (Sandbox Code Playgroud) 我找不到合适的解决方案.下面的代码给了我本地IP地址(如果我连接到Wifi,它提供的IP地址如192.168.0.x),但我想要公共IP地址(就像我在谷歌搜索"我的IP是什么")
public static String getLocalIpAddress() {
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() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
要么
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?谢谢!
我需要列出任何特定Android设备的所有可用IP地址.
我找到了一些示例代码,但这只会导致返回一个IP地址,这恰好是一个IPv6地址.我需要为任何特定设备获取所有可用的IP.我在这个应用程序的iOS版本上做同样的事情,它返回3个IPv6地址,一个192.地址和一个10.地址.我试图在Android上复制相同的内容.我将所有值传递给数组并将其显示在列表中.
我的代码是:
public String getLocalIpAddress()
{
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()) {
IPAddresses.setText(inetAddress.getHostAddress().toString());
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
String LOG_TAG = null;
Log.e(LOG_TAG, ex.toString());
}
return null;
}
Run Code Online (Sandbox Code Playgroud) android ×5
ip ×2
ip-address ×2
networking ×2
android-wifi ×1
arrays ×1
eclipse ×1
http ×1
return ×1
wifimanager ×1