我想在 android 中获取 dns 服务器。这个线程说有一些 android.net.NetworkUtils 类,但我发现没有这样的类。在这里问了同样的问题,但没有解决。
问问题
49286 次
2 回答
3
这是一个有效的项目示例:
for (long i = start; i < end + 1; i++) {
hosts_done++;
HostBean host = new HostBean();
host.ipAddress = NetInfo.getIpFromLongUnsigned(i);
try {
InetAddress ia = InetAddress.getByName(host.ipAddress);
host.hostname = ia.getCanonicalHostName();
host.isAlive = ia.isReachable(timeout) ? 1 : 0;
} catch (java.net.UnknownHostException e) {
Log.e(TAG, e.getMessage());
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
if (host.hostname != null && !host.hostname.equals(host.ipAddress)) {
// Is gateway ?
if (discover.net.gatewayIp.equals(host.ipAddress)) {
host.deviceType = 1;
}
// Mac Addr
host.hardwareAddress = HardwareAddress.getHardwareAddress(host.ipAddress);
// NIC vendor
try {
host.nicVendor = HardwareAddress.getNicVendor(host.hardwareAddress);
} catch (SQLiteDatabaseCorruptException e) {
Log.e(TAG, e.getMessage());
}
publishProgress(host);
} else {
publishProgress((HostBean) null);
于 2012-01-12T18:28:02.757 回答
0
实际上,这看起来是一个很好的答案:
android.net.ConnectivityManager
将为您提供一系列 NetworkInfo 的 usinggetAllNetworkInfo()
. 然后用于android.net.NetworkUtils.runDhcp()
获取任何给定网络接口的 DhcpInfo -DhcpInfo structure
具有该接口的 IP 地址dns1
和dns2
该接口的 IP 地址(它们是表示 IP 地址的整数值)。
这是另一个很好的链接:
于 2012-01-12T18:33:33.517 回答