1

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.

Community
  • 1
  • 1
prongs
  • 9,422
  • 21
  • 67
  • 105

2 Answers2

3

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);
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
TryTryAgain
  • 7,632
  • 11
  • 46
  • 82
  • well, Actually I want to get dns addresses but many threads mentioned I have to get an object of DhcpInfo class that's what I meant by getting dhcp info. – prongs Jan 13 '12 at 17:28
0

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

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • did you read the question carefully? I said there is nothing like android.net.NetworkUtils I don't know why – prongs Jan 13 '12 at 17:27