安卓的java.net.UnknownHostException:主机是尚未解决的(战略问题) [英] Android java.net.UnknownHostException: Host is unresolved (strategy question)

查看:190
本文介绍了安卓的java.net.UnknownHostException:主机是尚未解决的(战略问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用一个后台进程定期(如每小时)连接到内容源在网络上检查更新的内容安卓code。随着新用户下载应用程序并运行它的第一次,它似乎(这只是一个似乎在那一刻),在这首轮的情况,因为DNS为我们的服务器无法在设备上已经缓存,那些最初的一系列连接失败,可怕的UnknownHostException:主机是没有解决,当然,应用程序稍后再尝试和(同样,好像),这是所有的工作 - 也许是因为OS有时间真正解决。地址。

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.

所以,我的问题(S)是:(1)做其他Android开发者看到此行为与他们部署的应用程序呢?第一次,一系列的工作,后来自己去主机未解决的问题。 (2)有没有人有热身的DNS,所以到说话,这样第一个真正的连接工作,更好的策略?或者你只是有一些回退,你会遇到这样的异常时,循环再试试?我正在考虑具有尝试从我们的服务器上获取一个小的文本文件,并有它只是循环,直到它得到它,也许(不知道这部分)阻塞其它传出的网络连接,直到成功为止一个单独的线程。

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

推荐答案

我已经在使用的HttpURLConnection碰到过这样的行为。我使用简单的解决办法 - 我只是使用任何的URL,然后执行以下code

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

有关我第一次在这里获得的UnknownHostException但是URL旁边的用途是成功的(DNS服务器返回正确的IP地址,我可以连接到服务器)。

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

这篇关于安卓的java.net.UnknownHostException:主机是尚未解决的(战略问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆