9

I have android code that uses a background process to routinely (e.g. hourly) connect to a content source on the web to check for updated content. As new users download the app and run it for the first time, it seems (and this is just a "seems at the moment) that in this first-run situation, because the DNS for our servers are not cached already on the device, those first series of connections fail with dreaded UnknownHostException: Host is unresolved. And of course, the application tries again later and (again, "it seems like") it is all working -- perhaps because the OS has had time to actually resolve the address.

So, my question(s) are: (1) Do other Android developers see this behavior with their deployed applications as well? First time, a series of "host unresolved" issues that work themselves out later. (2) Does anyone have a better strategy for "warming up the DNS" so-to-speak so that the first real connections work? or perhaps do you just re-try with some back-off looping when you encounter this exception? I was contemplating having a separate thread that tries to fetch a small text file from our server and have it just loop until it gets it and maybe (not sure about this part) block the other outgoing network connections until it succeeds.

In any event, I have read through a chunk of the answers to various similarly worded questions here on Stack Overflow and I just to assure everyone that

<uses-permission android:name="android.permission.INTERNET" />

is set in my Manifest file :)

sorens
  • 4,975
  • 3
  • 29
  • 52
  • 1
    We encountered the same problem. To analyze it, we implemented an icmp ping that sends packet to a default dns (8.8.8.8). What we observed was that even though the ip-ping test was successful (thus an Internet connection is available) we still encountered the UnknownHostException. Any idea what could be the reason for this? – icyerasor Jan 08 '13 at 17:56

1 Answers1

15

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).

bart
  • 2,378
  • 2
  • 19
  • 21
  • very interesting idea, bart. for the record, we are using HttpUrlConnection. thanks! I will give that a try and monitor to see if we see a reduced number of exceptions. – sorens Jul 21 '10 at 16:51
  • I had that same problem last night with the google search api... it kept returning host not found... any idea what would cause that? – androidworkz Jul 22 '10 at 14:10
  • I was experiencing this too but in the spirit of others encountering the problem they may like to hear that mine was caused by the SD Card not existing and having no local storage which gave this unresolved host error on my BufferedInputStream(urlConnection.getInputStream()) call. – sradforth May 31 '11 at 22:56
  • but it give the same exception if you are connected to wireless but modem is off or you can say that you are connected to network but there is no data transfer or some provider problem. so how can i differentiate this case with the case if originally the host doesn't exist. – varun bhardwaj Mar 29 '12 at 09:36