Rob Stevenson-Leggett Rob Stevenson-Leggett - 1 year ago 142
Java Question

Unresolved Host Exception Android

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) {
} finally {
if (entity!=null)
try {
} 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.

I'm developing with 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.

Any ideas?

Answer Source

I think the problem might be on the first line:

new HttpHost("http://" + ServiceWrapper.SERVER_HOST,ServiceWrapper.SERVER_PORT);

The HttpHost constructor expects a hostname as its first argument, not a hostname with a "http://" prefix.

Try removing "http://" and it should work:

new HttpHost(ServiceWrapper.SERVER_HOST,ServiceWrapper.SERVER_PORT);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download