1
Android
Web Service
Topics
• HttpClient API
• Invocation styles (in HttpClient API)
• Response Formats
HttpClient API
HTTP Operations
• Based on Apache HTTP package
• HttpClient
> Interface for an HTTP client
> HTTP clients encapsulate a sm...
Invocation Styles
in HttpClient
Invocation Styles in HttpClient
• Synchronous
> public abstract HttpResponse execute
(HttpUriRequest request)
• Asynchrono...
Sending HTTP Request
• HttpRequest
• HttpGet
> Provides HTTP Get
• HttpPost
> Provides HTTP Post
Receiving HTTP Response
• HttpResponse
> Deals with responses in HTTP MIME type
• RespondHandler interface
> Handler that ...
Example Code - Synchronous
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = ...
Example Code - Asynchronous
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget =...
Response Formats
Response Formats
• XML
• JSON
• RSS, Atom
• ...
XML Parsing
• SAX
• DOM
• Pull-parser
JSON Parsing
• Use org.json.JSONObject class
• Example
JSONObject json = new JSONObject(result);
// A Simple JSONObject Pa...
Thank you
Upcoming SlideShare
Loading in...5
×

Android webservices

4,795

Published on

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,795
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
211
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Transcript of "Android webservices"

  1. 1. 1 Android Web Service
  2. 2. Topics • HttpClient API • Invocation styles (in HttpClient API) • Response Formats
  3. 3. HttpClient API
  4. 4. HTTP Operations • Based on Apache HTTP package • HttpClient > Interface for an HTTP client > HTTP clients encapsulate a smorgasbord of objects required to execute HTTP requests while handling cookies, authentication, connection management, and other features > Thread safety of HTTP clients depends on the implementation and configuration of the specific client • DefaultHttpClient > Default implementation of HTTP client
  5. 5. Invocation Styles in HttpClient
  6. 6. Invocation Styles in HttpClient • Synchronous > public abstract HttpResponse execute (HttpUriRequest request) • Asynchronous > public abstract T execute (HttpUriRequest request, ResponseHandler<? extends T> responseHandler)
  7. 7. Sending HTTP Request • HttpRequest • HttpGet > Provides HTTP Get • HttpPost > Provides HTTP Post
  8. 8. Receiving HTTP Response • HttpResponse > Deals with responses in HTTP MIME type • RespondHandler interface > Handler that encapsulates the process of generating a response object from a HttpResponse • BasicResponseHandler > A ResponseHandler that returns the response body as a String for successful (2xx) responses. If the response code was >= 300, the response body is consumed and an HttpResponseException is thrown.
  9. 9. Example Code - Synchronous HttpClient httpclient = new DefaultHttpClient(); // Prepare a request object HttpGet httpget = new HttpGet(url); // Execute the request HttpResponse response; try { response = httpclient.execute(httpget); // Get hold of the response entity HttpEntity entity = response.getEntity(); if (entity != null) { // A Simple JSON Response Read InputStream instream = entity.getContent(); result = convertStreamToString(instream);
  10. 10. Example Code - Asynchronous HttpClient httpclient = new DefaultHttpClient(); // Prepare a request object HttpGet httpget = new HttpGet(url); try { ResponseHandler<String> mResponseHandler = new BasicResponseHandler(); result = httpclient.execute(httpget, mResponseHandler); ...
  11. 11. Response Formats
  12. 12. Response Formats • XML • JSON • RSS, Atom • ...
  13. 13. XML Parsing • SAX • DOM • Pull-parser
  14. 14. JSON Parsing • Use org.json.JSONObject class • Example JSONObject json = new JSONObject(result); // A Simple JSONObject Parsing JSONArray nameArray = json.names(); JSONArray valArray = json.toJSONArray(nameArray); // A Simple JSONObject Value Pushing json.put("sample key", "sample value"); // A simple access to the first name jsonResult = nameArray.getString(0);
  15. 15. Thank you
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×