SlideShare una empresa de Scribd logo
1 de 15
Descargar para leer sin conexión
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 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
Invocation Styles
in HttpClient
Invocation Styles in HttpClient
• Synchronous
> public abstract HttpResponse execute
(HttpUriRequest request)
• Asynchronous
> public abstract T execute (HttpUriRequest request,
ResponseHandler<? extends T> responseHandler)
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 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.
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);
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);
...
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 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);
Thank you

Más contenido relacionado

La actualidad más candente (20)

React native
React nativeReact native
React native
 
Android UI
Android UIAndroid UI
Android UI
 
ReactJS presentation.pptx
ReactJS presentation.pptxReactJS presentation.pptx
ReactJS presentation.pptx
 
Event handling
Event handlingEvent handling
Event handling
 
Android Architecture
Android ArchitectureAndroid Architecture
Android Architecture
 
Soap vs rest
Soap vs restSoap vs rest
Soap vs rest
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Simple object access protocol(soap )
Simple object access protocol(soap )Simple object access protocol(soap )
Simple object access protocol(soap )
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and Sessions
 
Fragment
Fragment Fragment
Fragment
 
Android resource
Android resourceAndroid resource
Android resource
 
Introduction to ASP.NET
Introduction to ASP.NETIntroduction to ASP.NET
Introduction to ASP.NET
 
Asp.net.
Asp.net.Asp.net.
Asp.net.
 
ReactJS presentation
ReactJS presentationReactJS presentation
ReactJS presentation
 
SOAP vs REST
SOAP vs RESTSOAP vs REST
SOAP vs REST
 
JavaScript - Chapter 1 - Problem Solving
 JavaScript - Chapter 1 - Problem Solving JavaScript - Chapter 1 - Problem Solving
JavaScript - Chapter 1 - Problem Solving
 
Intro to web services
Intro to web servicesIntro to web services
Intro to web services
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
Introduction to Android development - Presentation
Introduction to Android development - PresentationIntroduction to Android development - Presentation
Introduction to Android development - Presentation
 

Destacado

Webservice for android ppt
Webservice for android pptWebservice for android ppt
Webservice for android pptsantosh lamba
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The BasicsJeff Fox
 
Consuming Web Services in Android
Consuming Web Services in AndroidConsuming Web Services in Android
Consuming Web Services in AndroidDavid Truxall
 
Anatomizing online payment systems: hack to shop
Anatomizing online payment systems: hack to shopAnatomizing online payment systems: hack to shop
Anatomizing online payment systems: hack to shopAbhinav Mishra
 
Tips dan Third Party Library untuk Android - Part 1
Tips dan Third Party Library untuk Android - Part 1Tips dan Third Party Library untuk Android - Part 1
Tips dan Third Party Library untuk Android - Part 1Ibnu Sina Wardy
 
Sandbox Introduction
Sandbox IntroductionSandbox Introduction
Sandbox Introductionmsimkin
 
Android permission system
Android permission systemAndroid permission system
Android permission systemShivang Goel
 
Android training day 4
Android training day 4Android training day 4
Android training day 4Vivek Bhusal
 
Security threats in Android OS + App Permissions
Security threats in Android OS + App PermissionsSecurity threats in Android OS + App Permissions
Security threats in Android OS + App PermissionsHariharan Ganesan
 
Android permission system
Android permission systemAndroid permission system
Android permission systemShivang Goel
 
Web Services and Android - OSSPAC 2009
Web Services and Android - OSSPAC 2009Web Services and Android - OSSPAC 2009
Web Services and Android - OSSPAC 2009sullis
 
Android secuirty permission - upload
Android secuirty   permission - uploadAndroid secuirty   permission - upload
Android secuirty permission - uploadBin Yang
 
Android AsyncTask Tutorial
Android AsyncTask TutorialAndroid AsyncTask Tutorial
Android AsyncTask TutorialPerfect APK
 
Android 6.0 permission change
Android 6.0 permission changeAndroid 6.0 permission change
Android 6.0 permission change彥彬 洪
 

Destacado (20)

Webservice for android ppt
Webservice for android pptWebservice for android ppt
Webservice for android ppt
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
 
Consuming Web Services in Android
Consuming Web Services in AndroidConsuming Web Services in Android
Consuming Web Services in Android
 
What is an API?
What is an API?What is an API?
What is an API?
 
OAuth: Trust Issues
OAuth: Trust IssuesOAuth: Trust Issues
OAuth: Trust Issues
 
Anatomizing online payment systems: hack to shop
Anatomizing online payment systems: hack to shopAnatomizing online payment systems: hack to shop
Anatomizing online payment systems: hack to shop
 
Tips dan Third Party Library untuk Android - Part 1
Tips dan Third Party Library untuk Android - Part 1Tips dan Third Party Library untuk Android - Part 1
Tips dan Third Party Library untuk Android - Part 1
 
Sandbox Introduction
Sandbox IntroductionSandbox Introduction
Sandbox Introduction
 
Android permission system
Android permission systemAndroid permission system
Android permission system
 
Android training day 4
Android training day 4Android training day 4
Android training day 4
 
Security threats in Android OS + App Permissions
Security threats in Android OS + App PermissionsSecurity threats in Android OS + App Permissions
Security threats in Android OS + App Permissions
 
Android(1)
Android(1)Android(1)
Android(1)
 
Android permission system
Android permission systemAndroid permission system
Android permission system
 
Web Services and Android - OSSPAC 2009
Web Services and Android - OSSPAC 2009Web Services and Android - OSSPAC 2009
Web Services and Android - OSSPAC 2009
 
Android secuirty permission - upload
Android secuirty   permission - uploadAndroid secuirty   permission - upload
Android secuirty permission - upload
 
Json Tutorial
Json TutorialJson Tutorial
Json Tutorial
 
Android AsyncTask Tutorial
Android AsyncTask TutorialAndroid AsyncTask Tutorial
Android AsyncTask Tutorial
 
Android 6.0 permission change
Android 6.0 permission changeAndroid 6.0 permission change
Android 6.0 permission change
 
Basic Android Push Notification
Basic Android Push NotificationBasic Android Push Notification
Basic Android Push Notification
 
Android new permission model
Android new permission modelAndroid new permission model
Android new permission model
 

Similar a Android webservices

Java Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet BasicJava Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet BasicIMC Institute
 
13 networking, mobile services, and authentication
13   networking, mobile services, and authentication13   networking, mobile services, and authentication
13 networking, mobile services, and authenticationWindowsPhoneRocks
 
Angular 4 The new Http Client Module
Angular 4 The new Http Client ModuleAngular 4 The new Http Client Module
Angular 4 The new Http Client Modulearjun singh
 
Client server part 12
Client server part 12Client server part 12
Client server part 12fadlihulopi
 
Java web programming
Java web programmingJava web programming
Java web programmingChing Yi Chan
 
Unit-5.pptx
Unit-5.pptxUnit-5.pptx
Unit-5.pptxitzkuu01
 
Java Servlets.pdf
Java Servlets.pdfJava Servlets.pdf
Java Servlets.pdfArumugam90
 
Android Lab Test : Using the network with HTTP (english)
Android Lab Test : Using the network with HTTP (english)Android Lab Test : Using the network with HTTP (english)
Android Lab Test : Using the network with HTTP (english)Bruno Delb
 
Introduction to Ajax programming
Introduction to Ajax programmingIntroduction to Ajax programming
Introduction to Ajax programmingFulvio Corno
 

Similar a Android webservices (20)

servlets
servletsservlets
servlets
 
Java Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet BasicJava Web Programming [2/9] : Servlet Basic
Java Web Programming [2/9] : Servlet Basic
 
13 networking, mobile services, and authentication
13   networking, mobile services, and authentication13   networking, mobile services, and authentication
13 networking, mobile services, and authentication
 
Angular 4 The new Http Client Module
Angular 4 The new Http Client ModuleAngular 4 The new Http Client Module
Angular 4 The new Http Client Module
 
Web Server.pdf
Web Server.pdfWeb Server.pdf
Web Server.pdf
 
Android dev 3
Android dev 3Android dev 3
Android dev 3
 
AJAX
AJAXAJAX
AJAX
 
AJAX
AJAXAJAX
AJAX
 
Client server part 12
Client server part 12Client server part 12
Client server part 12
 
Ajax
AjaxAjax
Ajax
 
Servlets intro
Servlets introServlets intro
Servlets intro
 
httprqstobj.ppt
httprqstobj.ppthttprqstobj.ppt
httprqstobj.ppt
 
Java web programming
Java web programmingJava web programming
Java web programming
 
Unit-5.pptx
Unit-5.pptxUnit-5.pptx
Unit-5.pptx
 
Servlets
ServletsServlets
Servlets
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Ajax
AjaxAjax
Ajax
 
Java Servlets.pdf
Java Servlets.pdfJava Servlets.pdf
Java Servlets.pdf
 
Android Lab Test : Using the network with HTTP (english)
Android Lab Test : Using the network with HTTP (english)Android Lab Test : Using the network with HTTP (english)
Android Lab Test : Using the network with HTTP (english)
 
Introduction to Ajax programming
Introduction to Ajax programmingIntroduction to Ajax programming
Introduction to Ajax programming
 

Más de Krazy Koder (20)

2310 b xd
2310 b xd2310 b xd
2310 b xd
 
2310 b xd
2310 b xd2310 b xd
2310 b xd
 
2310 b xd
2310 b xd2310 b xd
2310 b xd
 
2310 b xc
2310 b xc2310 b xc
2310 b xc
 
2310 b xb
2310 b xb2310 b xb
2310 b xb
 
2310 b 17
2310 b 172310 b 17
2310 b 17
 
2310 b 16
2310 b 162310 b 16
2310 b 16
 
2310 b 16
2310 b 162310 b 16
2310 b 16
 
2310 b 15
2310 b 152310 b 15
2310 b 15
 
2310 b 15
2310 b 152310 b 15
2310 b 15
 
2310 b 14
2310 b 142310 b 14
2310 b 14
 
2310 b 13
2310 b 132310 b 13
2310 b 13
 
2310 b 12
2310 b 122310 b 12
2310 b 12
 
2310 b 11
2310 b 112310 b 11
2310 b 11
 
2310 b 10
2310 b 102310 b 10
2310 b 10
 
2310 b 09
2310 b 092310 b 09
2310 b 09
 
2310 b 08
2310 b 082310 b 08
2310 b 08
 
2310 b 08
2310 b 082310 b 08
2310 b 08
 
2310 b 08
2310 b 082310 b 08
2310 b 08
 
2310 b 07
2310 b 072310 b 07
2310 b 07
 

Android webservices

  • 2. Topics • HttpClient API • Invocation styles (in HttpClient API) • Response Formats
  • 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
  • 6. Invocation Styles in HttpClient • Synchronous > public abstract HttpResponse execute (HttpUriRequest request) • Asynchronous > public abstract T execute (HttpUriRequest request, ResponseHandler<? extends T> responseHandler)
  • 7. Sending HTTP Request • HttpRequest • HttpGet > Provides HTTP Get • HttpPost > Provides HTTP Post
  • 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. 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. 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); ...
  • 12. Response Formats • XML • JSON • RSS, Atom • ...
  • 13. XML Parsing • SAX • DOM • Pull-parser
  • 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);