Android java.net.UnknownHostException:主机未解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3514398/
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
提问by Russ Bateman
This code doesn't work:
此代码不起作用:
URL url = new URL( xmlPath );
InputSource input = new InputSource( url.openStream() );
all the time, resulting in an UnknownHostException, even when the host exists, has been hit successfully using the same code if from a different development host (machine), and also from a browser.
一直导致 UnknownHostException,即使主机存在,如果来自不同的开发主机(机器)和浏览器,也使用相同的代码成功命中。
I'm probably out of line asking this question again, a repeat of Android java.net.UnknownHostException: Host is unresolved (strategy question). The forum software would not allow me to comment on the unsatisfactory answer to that question (in order to solicit more attention to the solution I ultimately found). Perhaps I'm not popular enough yet to be given that privilege. In the meantime, I'm essentially re-posting the question and a different answer.
我可能会再次问这个问题,重复Android java.net.UnknownHostException: Host is unresolved (strategy question)。论坛软件不允许我对那个问题的不满意答案发表评论(为了引起对我最终找到的解决方案的更多关注)。也许我还不够受欢迎,无法获得这种特权。与此同时,我基本上是重新发布问题和不同的答案。
Basically, I accept that maybe the Android device has a hard time with DNS under certain circumstances, but I've tried, for example the anddev.org example of how to use the SAX parser, and it worked on one machine I have and not on another.
基本上,我接受在某些情况下可能 Android 设备在使用 DNS 时遇到困难,但我已经尝试过,例如如何使用 SAX 解析器的 anddev.org 示例,它在我拥有的一台机器上运行,但没有另外一个。
Edit: As noted, I know a working answer and will post it.
编辑:如前所述,我知道一个有效的答案并将发布。
I am a) asking a question (that's already been asked and unsuccessfully answered) and b) answering it for the benefit of those for whom my answer will be useful.
我是 a) 提出一个问题(已经被问过但没有得到成功回答)并且 b) 回答它是为了那些我的回答对他们有用的人的利益。
回答by Russ Bateman
The answer is devilishly simple: remove, then re-create your AVD (virtual device/emulator) in Eclipse. It worked for me--first time.
答案非常简单:删除,然后在 Eclipse 中重新创建您的 AVD(虚拟设备/模拟器)。它对我有用 - 第一次。
回答by yancyn
I been follow the steps above but still try no luck, finally i just realize my app did not allow to go outside internet in the emulator. So just add users permissions in your manifest file. Done! Thanks to jozsi see http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device
我一直按照上面的步骤操作,但仍然没有尝试运气,最后我才意识到我的应用程序不允许在模拟器中访问互联网。因此,只需在清单文件中添加用户权限即可。完毕!感谢 jozsi 看到http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device
- android.permission.INTERNET
- android.permission.ACCESS_NETWORK_STATE
- android.permission.READ_PHONE_STATE
- android.permission.INTERNET
- android.permission.ACCESS_NETWORK_STATE
- android.permission.READ_PHONE_STATE
回答by milesology
In my case I was downloading an XML file from a URL on my phone but was having the issue describe in this thread.
就我而言,我正在从手机上的 URL 下载 XML 文件,但遇到了此线程中描述的问题。
for fixing it, I not only needed to have the regular internet permission
为了修复它,我不仅需要拥有常规的互联网权限
<uses-permission android:name="android.permission.INTERNET" />
But I also needed
但我也需要
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
As a suggestion to see if your device may need the same thing, try navigating to the URL in your phone's browser to see if prompts about needing SD Card or something.
作为查看您的设备是否可能需要相同内容的建议,请尝试导航到手机浏览器中的 URL,以查看是否提示需要 SD 卡或其他内容。
Hope that helps others.
希望能帮助别人。
回答by beetstra
I have seen this on a phone as well (while doing USB debugging). The solution was to disable WiFi, and re-enable it.
我也在手机上看到过这个(在进行 USB 调试时)。解决方案是禁用WiFi,然后重新启用它。
回答by Rahul Shirphule
It work's for me by adding user's permission for Internet in AndroidMenifest.xml
file
通过在AndroidMenifest.xml
文件中添加用户对 Internet 的权限,它对我有用
<uses-permission android:name="android.permission.INTERNET"/>
回答by Julien
In my case the problem was elsewhere. I had to:
就我而言,问题出在别处。我不得不:
add
<uses-permission android:name="android.permission.INTERNET" />
setup a proxy server on my emulator :
Emulator > settings > wireless and network > Mobile Networks > Access Point Name > "the access point you have ;)"
. Here you can set all the proxy port url and login/pass you want.
添加
<uses-permission android:name="android.permission.INTERNET" />
在我的模拟器上设置代理服务器:
Emulator > settings > wireless and network > Mobile Networks > Access Point Name > "the access point you have ;)"
。在这里您可以设置您想要的所有代理端口 url 和登录/密码。
AND
和
- Call my network operation in a separate thread. The easy way was to implement an AsynchTask.
- 在单独的线程中调用我的网络操作。简单的方法是实现一个AsynchTask。
I found the solution here:
我在这里找到了解决方案:
HTTP doesn't work in Android emulator
" This exception is there for a reason. Network activity can take time, performing networking on the main thread, which is the same thread that responsible for updating the UI, would freeze the thread until the networking is done (this is what happens on every thread, but when it's performed on a dedicated thread, it's ok). In Android, if the UI-thread isn't active for 5 seconds, it will show the Application is not responsive, Do you want to close it? dialog. "
" 这个异常是有原因的。网络活动可能需要时间,在主线程上执行网络,主线程是负责更新 UI 的同一个线程,会冻结线程,直到网络完成(这是每个线程,但是当它在专用线程上执行时,就可以了。在 Android 中,如果 UI 线程在 5 秒内未处于活动状态,它将显示应用程序没有响应,是否要关闭它?对话框。”
回答by Pratik Popat
You need to enable internet in your emulator.
您需要在模拟器中启用互联网。
Open emulator
Press menu button and open "settings"
Open "wireles and network"
Open "Mobile Networks"
Check on "Data enable"
打开模拟器
按菜单按钮并打开“设置”
打开“无线和网络”
打开“移动网络”
勾选“数据启用”
Now test your application again.
现在再次测试您的应用程序。
回答by Vito Andolini
I couldn't get rid of the emulator "UnknownHostExceptions" - and the emulator's browser wouldn't get a connection either. Tried all suggestions I could find at stack overflow and everywhere else.
我无法摆脱模拟器“UnknownHostExceptions” - 模拟器的浏览器也无法连接。尝试了我可以在堆栈溢出和其他任何地方找到的所有建议。
Finally I deleted my AVD, shut down Eclipse, and then created the AVD via the command line. Then launched it via command line with the -dns-server 8.8.8.8. (on my Mac: ./emulator -avd Google-8 -dns-server 8.8.8.8) It finally worked!
最后我删除了我的 AVD,关闭了 Eclipse,然后通过命令行创建了 AVD。然后通过命令行使用 -dns-server 8.8.8.8 启动它。(在我的 Mac 上:./emulator -avd Google-8 -dns-server 8.8.8.8)终于成功了!
Hope this helps someone else.
希望这对其他人有帮助。
回答by htafoya
My problem was when sharing the internet from the computer via WIFI, after ethernet connection was lost and reconnected I couldn't achieve connection on the phone. This was fixed by restarting internet sharing on computer.
我的问题是通过WIFI从计算机共享互联网时,以太网连接丢失并重新连接后,我无法在手机上实现连接。这是通过重新启动计算机上的互联网共享来解决的。
Hope it helps someone.
希望它可以帮助某人。
回答by Dan
Thank you so much Russ. I'm glad you persisted. I had spent the best part of an hour reading all kinds of possible fixes to this error. Just for anyone else stumbling across this thread, i'll briefly recap here: -Check Internet uses permission is in the manifest -Catching the exception (pointless) -Changing firewall settings on your OS -Share connection (windows only) And a load of other stuff
非常感谢罗斯。我很高兴你坚持了下来。我花了一个小时的大部分时间阅读针对此错误的各种可能的修复方法。对于在此线程中绊倒的其他人,我将在此处简要回顾一下: - 检查 Internet 使用权限是否在清单中 - 捕获异常(毫无意义) - 更改操作系统上的防火墙设置 - 共享连接(仅限 Windows)以及大量其他的东西
My fix took a little more than yours, but not much. After reading your post, I closed and destroyed all emulators. Then I noticed that when I moused over the run button (green circle with white arrow) it said the program was already running, even though no AVDs (emulators) existed! Anyway, restarted eclipse, created a AVD and ran it and Bob was my father's brother. Nothing to do with my code or setup at all.
我的修复比你的多一点,但不多。阅读您的帖子后,我关闭并销毁了所有模拟器。然后我注意到,当我将鼠标悬停在运行按钮(带白色箭头的绿色圆圈)上时,它表示程序已经在运行,即使没有 AVD(模拟器)存在!无论如何,重新启动 eclipse,创建一个 AVD 并运行它,Bob 是我父亲的兄弟。与我的代码或设置完全无关。
I found this afterwards. It may well achieve the same thing but I can't vouch for it unless the problem reoccurs. http://www.jiahaoliuliu.com/2011/02/stopping-android-applications-already.htmlAnother trick that /may/ achieve the same result is tick "wipe user data" in Run Configurations, Target tab.
我后来发现了这个。它可能会达到同样的目的,但除非问题再次发生,否则我不能保证它。 http://www.jiahaoliuliu.com/2011/02/stopping-android-applications-already.html/may/ 达到相同结果的另一个技巧是在“运行配置”、“目标”选项卡中勾选“擦除用户数据”。