简体   繁体   中英

How can I manually trigger a “Unable to resolve host” Exception on an Android Device

I am seeing a spike in these types of errors in my production app via logs:

"Unable to resolve host "{my.api.website}": No address associated with hostname"

The site / API works for 99.9% of people and I am only seeing this intermittently.

It appears as though it is an issue with internet connectivity as per these links:

I have therefore setup some retry logic so that it will make another attempt, but the problem is that I am unsure how to actually intentionally trigger this exception using an Android device (either emulator or live) to see if the new fix works.

How would I go about manually triggering a java.lang.exception where the site I am hitting throws this unable to resolve host issue?

You can trigger an UnknownHostException by spelling the host name incorrectly. The cause of the error won't be the same, but at least you'll get the same class of exception.

Out there "in the wild" this exception can happen for a number of reasons, including:

  • device loses connection to the network during or shortly before host name resolution
  • device connects to a captive portal that intercepts DNS requests or is not connected to the Internet
  • DNS request fails or times out for any other random reason

DNS request timeouts are a particularly insidious problem. They are difficult to reproduce. The default timeout is way too long, longer than users are willing to wait, and there's no way to change it. Developers have to resort to hacks like this: Can I set the getaddrinfo timeout in Android for DefaultHttpClient?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM