Pages
Calling REST Web Services with Android.
11:49 AM
Posted by Mina Samy
Requesting REST web service: you request REST web services by calling a URL with the parameters. like this
an example of calling a REST web service:
http://example.com/resources/getitems
an example of calling a REST web service:
String callWebErvice(String serviceURL){ // http get client HttpClient client=new DefaultHttpClient(); HttpGet getRequest=new HttpGet(); try { // construct a URI object getRequest.setURI(new URI(serviceURL)); } catch (URISyntaxException e) { Log.e("URISyntaxException", e.toString()); } // buffer reader to read the response BufferedReader in=null; // the service response HttpResponse response=null; try { // execute the request response = client.execute(getRequest); } catch (ClientProtocolException e) { Log.e("ClientProtocolException", e.toString()); } catch (IOException e) { Log.e("IO exception", e.toString()); } try { in=new BufferedReader(new InputStreamReader(response.getEntity().getContent())); } catch (IllegalStateException e) { Log.e("IllegalStateException", e.toString()); } catch (IOException e) { Log.e("IO exception", e.toString()); } StringBuffer buff=new StringBuffer(""); String line=""; try { while((line=in.readLine())!=null) { buff.append(line); } } catch (IOException e) { Log.e("IO exception", e.toString()); return e.getMessage(); } try { in.close(); } catch (IOException e) { Log.e("IO exception", e.toString()); } // response, need to be parsed return buff.toString(); }
Reactions: |
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
REST,
web service
. Follow any responses to this post through RSS. You can leave a response, or trackback from your own site.
Subscribe to:
Post Comments (Atom)
August 2, 2011 at 8:39 AM
in my scenario, i need to post a xml data with the request. I need to send the xml to insert a data into the database server behind the REST api. So, could you please modify the code and give me some hints how would I modify it?
August 13, 2011 at 12:30 PM
nice example thanks
February 25, 2012 at 9:14 AM
hello sir,
can u provide source code that demonstrate how to connect web service from android ,parsing content of web service and display the result on android emulator...