Android Java.Net.Unknownhostexception: Host Is Unresolved (Strategy Question)

Android java.net.UnknownHostException: Host is unresolved (strategy question)

I have come across this behaviour while using HttpUrlConnection. I am using simple workaround - I execute the following code just before using any url.

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

For the first time I obtain here UnknownHostException but next usages of the url are successful (DNS server returns proper IP address and I can connect to the server).

Android java.net.UnknownHostException: Host is unresolved

The answer is devilishly simple: remove, then re-create your AVD (virtual device/emulator) in Eclipse. It worked for me--first time.

java.lang.RuntimeException: java.net.UnknownHostException: Host is unresolved:

If this works in the emulator and not on your device it is most likely because you do not have InternetConnection on your device or the Feed URL that you are trying to access is not accessible from the Network that your device is connected too.

Android UnkownHostException on devices with a lower API level

I changed the hostname so that is doesn't contain a underscore, this resolved my issues. It seems that the DNS on older android versions does not support URLs with a underscore

Sources:

Similiar issue

http://code.google.com/p/android/issues/detail?id=37577

java.net.UnknownHostException: Unable to resolve host url: No address associated with hostname and End of input at character 0 of

I encountered this problem too, reconnecting the WiFi can solve this.

For us ,we can check if the phone can resolve the host to IP when we start application. If it cannot resolve, tell the user to check the WiFi and then exit.

I hope it helps.

java.net.UnknownHostException: Invalid hostname for server: local

What the exception is really saying is that there is no known server with the name "local". My guess is that you're trying to connect to your local computer. Try with the hostname "localhost" instead, or perhaps 127.0.0.1 or ::1 (the last one is IPv6).

From the javadocs:

Thrown to indicate that the IP address
of a host could not be determined.

127.0.0.1or ::1 or "localhost" should always be the loopback interface, so if that doesn't work I'd be really surprised.

If there really is a server called "local" on your network - examine your DNS settings or add it to your hosts file.

Does Android DNS need warm up?

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



Related Topics



Leave a reply



Submit