Android:未知主机异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3314137/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:40:40  来源:igfitidea点击:

Android: UnknownHostException

androidhttppostandroid-emulator

提问by anisbet

I am using Android SDK 2.2, testing my application with the emulator. I want to send a HTTP Post. When I do I get a UnknownHostException. I have placed the required permissions
<uses-permission android:name="android.permission.INTERNET" />
in the manifest.xml. Also I can open the browser on the emulator and navigate to the URL with no problem.

我正在使用 Android SDK 2.2,用模拟器测试我的应用程序。我想发送一个 HTTP 帖子。当我这样做时,我得到一个 UnknownHostException。我已将所需的权限
<uses-permission android:name="android.permission.INTERNET" />
放在 manifest.xml 中。此外,我可以在模拟器上打开浏览器并毫无问题地导航到 URL。

Here is my code:

这是我的代码:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost( uri );
HttpResponse response = null;
try
{
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
2 );
nameValuePairs.add( new BasicNameValuePair( "id", "edit-name" ) );
nameValuePairs
.add( new BasicNameValuePair( "stringdata", userName ) );
httppost.setEntity( new UrlEncodedFormEntity( nameValuePairs ) );

// Execute HTTP Post Request
response = httpclient.execute( httppost );
// Log.i( "HttpManager:", "======> response: "
// + response.getEntity().getContent() );

}
catch (ClientProtocolException e)
{
Log.e( "HttpManager", "ClientProtocolException thrown" + e );
}
catch (IOException e)
{
Log.e( "HttpManager", "IOException thrown" + e );
}

回答by anisbet

Ok, I feel pretty lame... The INTERNET permission tag is a child of the manifest tag, not the application tag. Sheesh!

好吧,我觉得很蹩脚... INTERNET 权限标记是清单标记的子项,而不是应用程序标记。嘘!

回答by Frank V

For others' consideration, I ran in to this problem and a Google landed me. As mentioned by anisbet, I double checked my permission tag and it was in the right spot.

为了其他人的考虑,我遇到了这个问题,谷歌找到了我。正如 anisbet 所提到的,我仔细检查了我的许可标签,它在正确的位置。

I eventually fired up the android built in browser and was getting the same response from my web server as well as Google.com (while the computer itself was fine). I terminated the android emulator and restarted; worked on the first try.

我最终启动了内置浏览器的 android,并从我的网络服务器和 Google.com 得到了相同的响应(而计算机本身很好)。我终止了android模拟器并重新启动;第一次尝试。

After reviewing your code, it may be worth while to restart the emulator. In all fairness to the emulator, a bunch of programs crashed shortly after doing this, so perhaps something else was going on in my computer. Still, this wasted a ton of time for me so perhaps this will save someone the headache I went though.

查看您的代码后,重新启动模拟器可能是值得的。平心而论,一堆程序在执行此操作后不久就崩溃了,所以我的计算机中可能发生了其他事情。尽管如此,这对我来说还是浪费了大量时间,所以也许这可以避免我头痛的问题。

Best of Luck!

祝你好运!

回答by igo

Make sure you have an internet connection. That's what happened to me when I forgot I am testing with mobile phone with no internet connection.

确保您有互联网连接。当我忘记我正在使用没有互联网连接的手机进行测试时,这就是发生在我身上的事情。

回答by Moritz

You know what solved it for me was putting the permission just before the closing manifest tag, like so:

您知道为我解决的问题是将许可放在结束清单标签之前,如下所示:

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

回答by java dev

It happens sometimes when you are running app in the emulator. Just restart the emulator will solve the problem. It worked for me !

当您在模拟器中运行应用程序时,有时会发生这种情况。只需重新启动模拟器即可解决问题。它对我有用!

回答by diadyne

If none of the above worked, try taking a step back and making sure that your device or emulator can actually reach the internet by opening up a browser.

如果上述方法均无效,请尝试退后一步,确保您的设备或模拟器可以通过打开浏览器实际访问互联网。

回答by Vijay

I ran in to the same issue. I have the correct permissions in my Android Manifest file and the Url is correct too. I am getting the response in the web browser. I restarted the IDE, Emulator, but didn't fix the problem. So i deleted the AVD using AVD manager and then started the emulator and it started to work.

我遇到了同样的问题。我在我的 Android 清单文件中拥有正确的权限,并且 URL 也是正确的。我在网络浏览器中收到响应。我重新启动了 IDE、模拟器,但没有解决问题。所以我使用 AVD 管理器删除了 AVD,然后启动了模拟器,它开始工作。

回答by metric152

I ran into a similar problem when testing an app that had a minSdkVersion set to 4 and I was trying to run it on a G1. Changing it to 3 solved the problem for me.

我在测试一个 minSdkVersion 设置为 4 的应用程序时遇到了类似的问题,我试图在 G1 上运行它。将其更改为 3 为我解决了问题。

回答by tlunter

A final check would be that your domain name is a valid domain. Having a underscore in a domain is invalid and will throw an unknown host exception.

最后的检查是您的域名是一个有效的域。在域中使用下划线是无效的,并且会抛出一个未知的主机异常。

回答by Tejaswi Yerukalapudi

One other thing: It turned out that the internet itself wasn't working for me. Launching the emulator from the commandline with these switches fixed it for me: emulator -avd your_avd_name -dns-server 8.8.8.8

另一件事:事实证明,互联网本身并不适合我。使用这些开关从命令行启动模拟器为我修复了它:emulator -avd your_avd_name -dns-server 8.8.8.8