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?
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.
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.
Thrown to indicate that the IP address of a host could not be determined.
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With