java 未解决的主机异常 Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/992880/
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
Unresolved Host Exception Android
提问by Rob Stevenson-Leggett
I'm trying to call a RESTful web service from an Android application using the following method:
我正在尝试使用以下方法从 Android 应用程序调用 RESTful Web 服务:
HttpHost target = new HttpHost("http://" + ServiceWrapper.SERVER_HOST,ServiceWrapper.SERVER_PORT);
HttpGet get = new HttpGet("/list");
String result = null;
HttpEntity entity = null;
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(target, get);
entity = response.getEntity();
result = EntityUtils.toString(entity);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (entity!=null)
try {
entity.consumeContent();
} catch (IOException e) {}
}
return result;
I can browse to address and see the xml results using the Android Emulator browser and from my machine. I have given my app the INTERNET permission.
我可以使用 Android Emulator 浏览器和我的机器浏览到地址并查看 xml 结果。我已授予我的应用 INTERNET 权限。
I'm developing with eclipse.
我正在用eclipse开发。
I've seen it mentioned that I might need to configure a proxy but since the web service I'm calling is on port 80 this shouldn't matter should it? I can call the method with the browser.
我已经看到它提到我可能需要配置一个代理,但是由于我正在调用的 Web 服务在端口 80 上,这应该无关紧要吗?我可以使用浏览器调用该方法。
Any ideas?
有任何想法吗?
回答by Josef Pfleger
I think the problem might be on the first line:
我认为问题可能出在第一行:
new HttpHost("http://" + ServiceWrapper.SERVER_HOST,ServiceWrapper.SERVER_PORT);
The HttpHostconstructor expects a hostname as its first argument, not a hostname with a "http://"prefix.
该HttpHost构造预期的主机名作为第一个参数,而不是用主机名"http://"前缀。
Try removing "http://"and it should work:
尝试删除"http://"它应该可以工作:
new HttpHost(ServiceWrapper.SERVER_HOST,ServiceWrapper.SERVER_PORT);
回答by jitter
The error means the URL can't be resolved via DNS. The are a many problems which might cause this. If you sit behind a proxy you should configure it to be used.
该错误意味着无法通过 DNS 解析 URL。有很多问题可能会导致这种情况。如果您坐在代理后面,您应该配置它以供使用。
HttpHost proxy = new HttpHost(”proxy”,port,”protocol”);
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
Also check that your internetpermission looks like this
还要检查您的 internetpermission 看起来像这样
<uses-permission android:name=”android.permission.INTERNET”></uses-permission>
As sometimes it won't work as empty tag.
因为有时它不能作为空标签工作。
Also check out Emulator Networkingand the limitations section there
另请查看模拟器网络和那里的限制部分
回答by hacken
I would double check that the network permission is set correctly. Try something basic like
我会仔细检查网络权限是否设置正确。尝试一些基本的东西
String address -"http://www.google.com";
URL url = new URL(address);
InputStream in = url.openStream();
to verify that you have your permissions set up right.
验证您的权限设置是否正确。
After that use your favorite protocol analyzer(I am a wireshark fan) to figure if you are sending out the right packets. I believe that you need to pass in the complete URL to HTTPGet but I am only about 80% sure.
之后使用你最喜欢的协议分析器(我是一个wireshark粉丝)来确定你是否发送了正确的数据包。我相信您需要将完整的 URL 传递给 HTTPGet,但我只有 80% 的把握。
回答by SRY
Change first line in your code:
更改代码中的第一行:
HttpHost(ServiceWrapper.SERVER_HOST,ServiceWrapper.SERVER_PORT);

