【问题标题】:how to get DNS Info in android [duplicate]如何在android中获取DNS信息[重复]
【发布时间】:2012-02-09 01:00:24
【问题描述】:

可能重复:
How do you get the current DNS servers for Android?

我想在 android 中获取 dns 服务器。 this thread 说有一些 android.net.NetworkUtils 类,但我发现没有这样的类。同样的问题被问到here,但没有得到解决。

【问题讨论】:

  • 那么,您需要有关 DHCP 或 DNS 的信息吗?这些是完全不同的东西。
  • @svick 我想要关于 dns 的信息。但不是wifi接口,而是gprs/3g接口。
  • @prongs 可以阐明标记答案如何帮助您获得 DNS 服务器?

标签: android dns dhcp android-networking


【解决方案1】:

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

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

【讨论】:

  • 好吧,其实我想获取 dns 地址,但很多线程提到我必须获取 DhcpInfo 类的对象,这就是我获取 dhcp 信息的意思。
【解决方案2】:

实际上,这看起来是个不错的答案:

How do you get the current DNS servers for Android?

android.net.ConnectivityManager 将为您提供一系列 NetworkInfo 使用getAllNetworkInfo()。然后使用 android.net.NetworkUtils.runDhcp() 获取任何给定的 DhcpInfo 网络接口 - DhcpInfo structure 的 IP 地址为 dns1dns2 用于该接口(它们是表示 IP 地址)。

这是另一个很好的链接:

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

【讨论】:

  • 您仔细阅读问题了吗?我说没有什么像android.net.NetworkUtils 我不知道为什么
猜你喜欢
  • 1970-01-01
  • 2012-09-30
  • 2017-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 2013-12-22
相关资源
最近更新 更多