Does Android DNS need a warm up?

From old posts such as Android java.net.UnknownHostException: the host is not resolved (a strategy issue), he suggested using the following code:

try { InetAddress i = InetAddress.getByName(URLName); } catch (UnknownHostException e1) { e1.printStackTrace(); } 

Does this mean when the DNS record is not cached in the device, for example. after loading it will return java.net.UnknownHostException: Host for the first time, even for valid DNS?

+6
source share
1 answer

It depends on the version of Android.

If you have an Internet connection and that your application states that it needs Internet access in the manifest file, then the address should be resolved 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 ask is not older than the lifetime, the cache will respond. If it is not in the cache or expired, the OS will try to find it by going to the DNS server. An exception is thrown only if the Internet connection is not made, or when there is no DNS response, and not when the cache request fails.

However, if you are writing your application for old androids, this problem may still bother you.

There are ways to handle this:
Android: DNS cleanup

+3
source

Source: https://habr.com/ru/post/927012/


All Articles