I have an Android device connected to Wifi network with an IP address. I did NS lookup on my Linux computer of this IP address and verified that there is a corresponding hostname for that IP address.
I have a piece of Java code, which when run on my Windows PC does reverse DNS lookup fine (returns a hostname):
String dnSuffix;
String ipAddress = "10.228.59.217";
InetAddress inetAddr;
try {
//inetAddr = InetAddress.getLocalHost();
inetAddr = InetAddress.getByName(ipAddress);
//System.out.println("inetAddr = " + inetAddr);
Log.v(LOG_TAG, "inetAddr = " + inetAddr);
if (inetAddr != null) {
dnSuffix = inetAddr.getHostName();
//System.out.println("dnSuffix is " + dnSuffix);
Log.v(LOG_TAG,"dnSuffix is " + dnSuffix);
}
} catch (UnknownHostException e) {
//System.out.println("Error getting DN suffix: " + e.getMessage());
Log.v(LOG_TAG,"Error getting DN suffix: " + e.getMessage());
}
dnSuffix
is the hostname as expected on Windows.
But on Android, it returns an IP address instead of a hostname, which indicates that it failed.
I have all the permissions that I would think I need for this in my app:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" ></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
I found this documentation:
getCanonicalHostName
public String getCanonicalHostName()
Gets the fully qualified domain name for this IP address. Best effort method, meaning we may not be able to return the FQDN depending on the underlying system configuration. If there is a security manager, this method first calls its checkConnect method with the hostname and -1 as its arguments to see if the calling code is allowed to know the hostname for this IP address, i.e., to connect to the host. If the operation is not allowed, it will return the textual representation of the IP address.
Returns:
the fully qualified domain name for this IP address, or if the operation is not allowed by the security check, the textual representation of the IP address.
Since:
1.4
See Also:
SecurityManager.checkConnect(java.lang.String, int)
I also found someone having similar problem:
getCanonicalHostName returns an IP address
But no resolution.
Can anyone provide any help at all?