Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved Host Exception Android

Tags:

java

rest

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?

like image 796
Rob Stevenson-Leggett Avatar asked Jun 14 '09 14:06

Rob Stevenson-Leggett


People also ask

How do I resolve an unknown host exception?

To resolve application level issues, try the following methods: Restart your application. Confirm that your Java application doesn't have a bad DNS cache. If possible, configure your application to adhere to the DNS TTL.

What causes unknown host exception?

The UnknownHostException occurs when trying to connect to a remote host using its hostname, but the IP address of the host could not be determined. This usually happens because of a typo in the hostname, or because of a DNS misconfiguration or propagation delay.

What is UnknownHostException in Android?

Thrown to indicate that the IP address of a host could not be determined.


2 Answers

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);
like image 148
Josef Pfleger Avatar answered Sep 20 '22 01:09

Josef Pfleger


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

like image 39
jitter Avatar answered Sep 20 '22 01:09

jitter