问题描述
我想用下面的方法来调用从一个Android应用程序RESTful Web服务:
I'm trying to call a RESTful web service from an Android application using the following method:
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;
我可以浏览到的地址,并使用Android模拟器浏览器,并从我的机器看的XML结果。我给我的应用程序的INTERNET权限。
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.
我正在开发的Eclipse。
I'm developing with eclipse.
我已经看到了它提到,我可能需要配置一个代理,但由于Web服务我打电话是在80端口,这应该不是问题应该吗?我可以调用该方法的浏览器。
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.
任何想法?
推荐答案
我想问题可能是在第一行:
I think the problem might be on the first line:
new HttpHost("http://" + ServiceWrapper.SERVER_HOST,ServiceWrapper.SERVER_PORT);
在 HttpHost
构造函数使用主机名作为第一个参数,而不是用主机名的http://
preFIX。
The HttpHost
constructor expects a hostname as its first argument, not a hostname with a "http://"
prefix.
尝试删除HTTP://
,它应该工作:
Try removing "http://"
and it should work:
new HttpHost(ServiceWrapper.SERVER_HOST,ServiceWrapper.SERVER_PORT);
这篇关于未解析主机异常的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!