tags:

views:

3752

answers:

5

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?

A: 

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

jitter
I'm not sure about the proxy settings as I'm only hitting port 80 and the emulator's browser can call the webservice.
Rob Stevenson-Leggett
+1  A: 

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.

hacken
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!
Rob Stevenson-Leggett
Is this method a valid way to call a REST web service? I.e. consuming the input stream into a string?
Rob Stevenson-Leggett
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.
hacken
Fiddler is a Web Debugging Proxy (i.e. a HTTP proxy server used to analyze HTTP traffic) and therefore not a packet sniffer.
Josef
Thanks for the info!
Rob Stevenson-Leggett
+5  A: 

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);
Josef
Awww so annoying :) Fixed. Thankyou.
Rob Stevenson-Leggett
A: 

Hi Rob and All,

Hope you all are doing great!!!

I m totally new to android and REST service, i have a scenario, that i want to display some contents from an ASP website, which is hosted live. My task is to write a REST service and a client application in androids to display some contents/results from that ASP website.

Anybody can help in this regard.

Thanx for ur cooperation.

Faheem Akbar Sial

[email protected]

Faheem Akbar Sial
A: 

Hi, How can I verify the ID and Password from my local website (based on asp) using android (in website I verify the Id and Password after checking the database)? If any one know the solution or have any source code please help me.

Asma-ull-Hosna
You should really ask this as a full question rather than as an answer for mine but I would create a .NET webservice on your website which can verify the username and password. Then call that from your android application.
Rob Stevenson-Leggett