简体   繁体   中英

Resolve hostname with specific DNS server on Android

I have a program that needs to ask a local machine for DNS lookups, but I can not modify the global DNS settings of the host. How would I go about doing this?

You can try to use dnsjava library

Here is a sample:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM