From old posts such as Android java.net.UnknownHostException: Host is unresolved (strategy question), it suggested to use the following code:
try {
InetAddress i = InetAddress.getByName(URLName);
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
So does it mean when a DNS entry is not being cached in the device, e.g. after bootup, it will return java.net.UnknownHostException: Host
for the 1st time, even for a valid DNS?
It depends on the version of android.
Provided you have an Internet connection and that your application declares that it needs Internet access in the manifest file, then the address should resolve without any problems.
http://developer.android.com/reference/java/net/InetAddress.html
In Android 4.0 (Ice Cream Sandwich) and earlier, DNS caching was performed both by InetAddress and by the C library, which meant that DNS TTLs could not be honored correctly. In later releases, caching is done solely by the C library and DNS TTLs are honored.
So if the address you are asking for is not older than the time to live, the cache will answer. If it is not in the cache or has expired then the OS will try to find it by going to a DNS server. The exception is thrown only when your Internet connection is not up, or when there is no DNS response, not when the cache request fails.
That said, if you are writing your application for older androids, then this problem may pester you still.
There are ways to deal with it:
Android: Flush DNS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With