未解决的主机异常Android

Rob*_*ett 9 java rest android

我正在尝试使用以下方法从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;
Run Code Online (Sandbox Code Playgroud)

我可以浏览地址并使用Android Emulator浏览器和我的机器查看xml结果.我已经给了我的应用程序INTERNET权限.

我正在用eclipse开发.

我已经看到它提到我可能需要配置一个代理,但由于我正在调用的Web服务是在80端口,这应该不重要吗?我可以用浏览器调用该方法.

有任何想法吗?

Jos*_*ger 14

我认为问题可能在第一行:

new HttpHost("http://" + ServiceWrapper.SERVER_HOST,ServiceWrapper.SERVER_PORT);
Run Code Online (Sandbox Code Playgroud)

HttpHost构造预期的主机名作为第一个参数,而不是用主机名"http://"前缀.

尝试删除"http://"它应该工作:

new HttpHost(ServiceWrapper.SERVER_HOST,ServiceWrapper.SERVER_PORT);
Run Code Online (Sandbox Code Playgroud)