9

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?

2
  • Proxy settings won't matter unless your device is behind a proxy server. Commented Jun 14, 2009 at 20:53
  • That's what I was thinking. I still can't get this working. Commented Jun 16, 2009 at 8:59

4 Answers 4

14
+150

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);
Sign up to request clarification or add additional context in comments.

Comments

3

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

3 Comments

I'm not sure about the proxy settings as I'm only hitting port 80 and the emulator's browser can call the webservice.
This did it for me, when using a normal HttpGet("http: //host/path"); - Eclipse doesn't seem to put this XML tag in. Thank you!
<uses-permission android:name=”android.permission.INTERNET”></uses-permission> worked for me too.
1

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.

4 Comments

Right, I can get data back using this method. However I can't figure out why the original code doesn't work. I'm not seeing anything in Fiddler for either!
Is this method a valid way to call a REST web service? I.e. consuming the input stream into a string?
It is a very simple method but it might be adequate depending on what you are doing. I am not familiar with fiddler but I am not sure if it is the same as a packet sniffer. I am guessing the URL or host is wrong. I would try passing in the full URL ("http://" + ServiceWrapper.SERVER_HOST+"//list") and see if that helps.
Fiddler is a Web Debugging Proxy (i.e. a HTTP proxy server used to analyze HTTP traffic) and therefore not a packet sniffer.
0

Change first line in your code:

HttpHost(ServiceWrapper.SERVER_HOST,ServiceWrapper.SERVER_PORT);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.