Android java.net.UnknownHostException: Host is unresolved (策略问题)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3293659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android java.net.UnknownHostException: Host is unresolved (strategy question)
提问by sorens
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.
我有使用后台进程定期(例如每小时)连接到网络上的内容源以检查更新内容的 android 代码。当新用户第一次下载应用程序并运行它时,在第一次运行的情况下,似乎(这只是“目前看来”),因为我们服务器的 DNS 尚未缓存在设备上,第一系列连接失败并出现可怕的 UnknownHostException: Host is unresolved。当然,应用程序稍后再试一次,并且(再次,“似乎”)一切正常——也许是因为操作系统有时间实际解决问题地址。
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.
所以,我的问题是:(1) 其他 Android 开发人员是否也看到他们部署的应用程序的这种行为?第一次,一系列“主机未解决”的问题后来自行解决。(2) 有没有人有更好的策略来“预热 DNS”,可以这么说,以便第一个真正的连接工作?或者,当您遇到此异常时,您是否只是重新尝试一些回退循环?我正在考虑使用一个单独的线程来尝试从我们的服务器获取一个小文本文件,并让它循环直到它得到它,并且可能(不确定这部分)阻止其他传出网络连接,直到它成功。
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
无论如何,我已经阅读了 Stack Overflow 上各种措辞相似的问题的大部分答案,我只是向大家保证
<uses-permission android:name="android.permission.INTERNET" />
is set in my Manifest file :)
在我的清单文件中设置:)
采纳答案by bart
I have come across this behaviour while using HttpUrlConnection. I am using simple workaround - I execute the following code just before using any url.
我在使用 HttpUrlConnection 时遇到过这种行为。我正在使用简单的解决方法 - 我在使用任何 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).
我第一次在这里获得 UnknownHostException,但 url 的下一次使用是成功的(DNS 服务器返回正确的 IP 地址,我可以连接到服务器)。