Rob Stevenson-Leggett
Rob Stevenson-Leggett

Reputation: 35679

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) {
    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.

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?

Upvotes: 9

Views: 18680

Answers (4)

SRY
SRY

Reputation: 139

Change first line in your code:

HttpHost(ServiceWrapper.SERVER_HOST,ServiceWrapper.SERVER_PORT);

Upvotes: 0

Josef Pfleger
Josef Pfleger

Reputation: 74527

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);

Upvotes: 14

hacken
hacken

Reputation: 2145

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.

Upvotes: 1

jitter
jitter

Reputation: 54605

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.

HttpHost proxy = new HttpHost(”proxy”,port,”protocol”);
client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

Also check that your internetpermission looks like this

<uses-permission android:name=”android.permission.INTERNET”></uses-permission>

As sometimes it won't work as empty tag.

Also check out Emulator Networking and the limitations section there

Upvotes: 3

Related Questions