我正在尝试使用以下方法从 Android 应用程序调用 RESTful 网络服务:
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 Emulator 浏览器和我的机器浏览地址并查看 xml 结果。我已授予我的应用程序 INTERNET 权限。
我正在使用 eclipse 进行开发。
我看到它提到我可能需要配置代理,但由于我调用的 Web 服务在端口 80 上,这应该无关紧要吧?我可以用浏览器调用该方法。
有什么想法吗?
最佳答案
我认为问题可能出在第一行:
new HttpHost("http://" + ServiceWrapper.SERVER_HOST,ServiceWrapper.SERVER_PORT);
HttpHost
构造函数需要主机名作为其第一个参数,而不是带有 "http://"
前缀的主机名。
尝试删除 "http://"
,它应该可以工作:
new HttpHost(ServiceWrapper.SERVER_HOST,ServiceWrapper.SERVER_PORT);
关于java - Unresolved 主机异常 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/992880/