android webservices

15
1 Android Web Service

Upload: krazy-koder

Post on 22-Nov-2014

5.106 views

Category:

Documents


5 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Android webservices

1

Android Web Service

Page 2: Android webservices

Topics• HttpClient API• Invocation styles (in HttpClient API)• Response Formats

Page 3: Android webservices

HttpClient API

Page 4: Android webservices

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

Page 5: Android webservices

Invocation Stylesin HttpClient

Page 6: Android webservices

Invocation Styles in HttpClient• Synchronous> public abstract HttpResponse execute

(HttpUriRequest request)• Asynchronous> public abstract T execute (HttpUriRequest request,

ResponseHandler<? extends T> responseHandler)

Page 7: Android webservices

Sending HTTP Request• HttpRequest• HttpGet> Provides HTTP Get

• HttpPost> Provides HTTP Post

Page 8: Android webservices

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.

Page 9: Android webservices

Example Code - SynchronousHttpClient httpclient = new DefaultHttpClient();

// Prepare a request objectHttpGet httpget = new HttpGet(url);

// Execute the requestHttpResponse 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);

Page 10: Android webservices

Example Code - AsynchronousHttpClient httpclient = new DefaultHttpClient();

// Prepare a request objectHttpGet httpget = new HttpGet(url);

try {

ResponseHandler<String> mResponseHandler = new BasicResponseHandler(); result = httpclient.execute(httpget, mResponseHandler); ...

Page 11: Android webservices

Response Formats

Page 12: Android webservices

Response Formats• XML• JSON• RSS, Atom• ...

Page 13: Android webservices

XML Parsing• SAX• DOM• Pull-parser

Page 14: Android webservices

JSON Parsing• Use org.json.JSONObject class• Example

JSONObject json = new JSONObject(result);

// A Simple JSONObject ParsingJSONArray nameArray = json.names();JSONArray valArray = json.toJSONArray(nameArray);

// A Simple JSONObject Value Pushingjson.put("sample key", "sample value");

// A simple access to the first namejsonResult = nameArray.getString(0);