SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Web services SOAP
Web services SOAPWeb services SOAP
Web services SOAP
 
Restful web services ppt
Restful web services pptRestful web services ppt
Restful web services ppt
 
What is Socket Programming in Python | Edureka
What is Socket Programming in Python | EdurekaWhat is Socket Programming in Python | Edureka
What is Socket Programming in Python | Edureka
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
IIS
IISIIS
IIS
 
Common Gateway Interface
Common Gateway InterfaceCommon Gateway Interface
Common Gateway Interface
 
Files in java
Files in javaFiles in java
Files in java
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Introduction to Web Architecture
Introduction to Web ArchitectureIntroduction to Web Architecture
Introduction to Web Architecture
 
Servlets api overview
Servlets api overviewServlets api overview
Servlets api overview
 
Java servlets
Java servletsJava servlets
Java servlets
 
Php
PhpPhp
Php
 
An Introduction To REST API
An Introduction To REST APIAn Introduction To REST API
An Introduction To REST API
 
Input output streams
Input output streamsInput output streams
Input output streams
 
Webservices
WebservicesWebservices
Webservices
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
 
PHP - Introduction to PHP Fundamentals
PHP -  Introduction to PHP FundamentalsPHP -  Introduction to PHP Fundamentals
PHP - Introduction to PHP Fundamentals
 
Scripting Languages
Scripting LanguagesScripting Languages
Scripting Languages
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
 

Andere mochten auch

Webservice for android ppt
Webservice for android pptWebservice for android ppt
Webservice for android pptsantosh lamba
 
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彥彬 洪
 

Andere mochten auch (20)

Webservice for android ppt
Webservice for android pptWebservice for android ppt
Webservice for android ppt
 
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
 
JSON overview and demo
JSON overview and demoJSON overview and demo
JSON overview and demo
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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);