简体   繁体   English

Android java.net.UnknownHostException:主机未解析(策略问题)

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

I have android code that uses a background process to routinely (eg hourly) connect to a content source on the web to check for updated content. 我有安卓代码,它使用后台进程定期(例如每小时)连接到Web上的内容源以检查更新的内容。 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. 当新用户第一次下载应用程序并运行它时,似乎(这只是“目前似乎”)在这种首次运行的情况下,因为我们的服务器的DNS尚未在设备上缓存,那些第一系列连接因可怕的UnknownHostException而失败:主机未解析。当然,应用程序稍后再次尝试(再次,“看起来像”)它一切正常 - 也许是因为操作系统有时间实际解决地址。

So, my question(s) are: (1) Do other Android developers see this behavior with their deployed applications as well? 所以,我的问题是:(1)其他Android开发人员是否也看到了他们部署的应用程序的这种行为? 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? (2)有没有人有更好的策略来“加热DNS”这么说,以便第一个真正的连接起作用? 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 无论如何,我已经在Stack Overflow上阅读了各种类似措辞问题的答案,我只是向大家保证

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

is set in my Manifest file :) 在我的清单文件中设置:)

I have come across this behaviour while using HttpUrlConnection. 我在使用HttpUrlConnection时遇到过这种行为。 I am using simple workaround - I execute the following code just before using any url. 我使用简单的解决方法 - 我在使用任何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地址,我可以连接到服务器)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.RuntimeException:java.net.UnknownHostException:主机未解析: - java.lang.RuntimeException: java.net.UnknownHostException: Host is unresolved: Android java.net.UnknownHostException:无法解析主机:server_address:443 - Android java.net.UnknownHostException: Host is unresolved: server_address:443 的java.net.UnknownHostException - java.net.UnknownHostException Android 6.0:java.net.UnknownHostException:无法解析主机。 在Android 7.0上正常工作 - Android 6.0 : java.net.UnknownHostException: Unable to resolve host. Working fine in Android 7.0 java.net.UnknownHostException:无法解析主机“@MYIPADDRESS,当使用android + mongodb时 - java.net.UnknownHostException: Unable to resolve host "@MYIPADDRESS, when using android + mongodb 重新启动java.net时android应用程序崩溃。UnknownHostException:无法解析主机 - android app crashes on reboot java.net.UnknownHostException: Unable to resolve host Android API 23 java.net.UnknownHostException:无法解析主机“URL”:没有与主机名关联的地址 - Android API 23 java.net.UnknownHostException: Unable to resolve host “URL”: No address associated with hostname java.net.UnknownHostException:无法解析主机没有与主机名关联的地址 - java.net.UnknownHostException: Unable to resolve host No address associated with hostname 引起:java.net.UnknownHostException:host.docker.internal - Caused by: java.net.UnknownHostException: host.docker.internal java.net.UnknownHostException-Jsoup - java.net.UnknownHostException - Jsoup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM