如何在android中获取DNS信息

发布于 2024-12-26 05:28:00 字数 650 浏览 2 评论 0原文

可能的重复:
如何获取 Android 的当前 DNS 服务器?< /a>

我想获取 android 中的 dns 服务器。 此线程说有一些 android.net.NetworkUtils 类,但我发现没有这样的类。 这里也提出了同样的问题,但它是未解决。

Possible Duplicate:
How do you get the current DNS servers for Android?

I want to get hold of the dns servers in android. this thread says there is some android.net.NetworkUtils class but I found out that there's no such class. Same question was asked here but it was unresolved.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏剧牡丹亭 2025-01-02 05:28:00

这是一个有效的项目示例:

https://github.com/rorist/android-network-discovery/blob/master/src/info/lamatricexiste/network/DnsDiscovery.java

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);

Here's a Project example that works:

https://github.com/rorist/android-network-discovery/blob/master/src/info/lamatricexiste/network/DnsDiscovery.java

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);
花间憩 2025-01-02 05:28:00

实际上,这看起来是一个很好的答案:

你怎么样获取 Android 当前的 DNS 服务器?

android.net.ConnectivityManager 将为您提供一组
NetworkInfo 使用 getAllNetworkInfo()。然后使用
android.net.NetworkUtils.runDhcp() 获取任何给定的 DhcpInfo
网络接口 - DhcpInfo 结构 具有 dns1 的 IP 地址
和该接口的 dns2 (它们是表示
IP 地址)。

这是另一个很好的链接:

http://www.devdaily.com/java/jwarehouse/android/wifi/java/android/net/wifi/WifiStateTracker.java.shtml

Actually, this looks like a good answer:

How do you get the current DNS servers for Android?

android.net.ConnectivityManager will deliver you an array of
NetworkInfo's using getAllNetworkInfo(). Then use
android.net.NetworkUtils.runDhcp() to get a DhcpInfo for any given
network interface - the DhcpInfo structure has the IP address for dns1
and dns2 for that interface (which are integer values representing the
IP address).

Here is another good link:

http://www.devdaily.com/java/jwarehouse/android/wifi/java/android/net/wifi/WifiStateTracker.java.shtml

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文