使用Android上的特定DNS服务器解析主机名

时间:2015-01-21 23:47:10

标签: android dns

我有一个程序需要向本地计算机请求DNS查找,但我无法修改主机的全局DNS设置。我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用dnsjava

以下是一个示例:

private static String resolveDomain(String domain, String serverName) {
    String hostName = null;
    Lookup lookup;

    try {
        lookup = new Lookup(domain);
        Resolver resolver = new SimpleResolver(serverName);
        lookup.setResolver(resolver);
        Record recs[] = lookup.run();
        if (recs == null) {
            return null;
        }
        for (Record rec : recs) {
            hostName = rec.getName().toString();
            break;
        }
    } catch (TextParseException e) {
    } catch (NullPointerException e) {
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (hostName == null) {
        return null;
    } else if (hostName.endsWith(".")) {
        hostName = hostName.substring(0, hostName.length() - 1);
    }
    return hostName;
}