1 / 27

Mastering Web API for Mobile: Best Practices & Implementation Tips

Discover the history, advantages, and practical usage of Web API for mobile applications, with a focus on REST vs. SOAP, JSON vs. XML, public APIs, HTTP methods, security measures, consuming APIs in iOS and Android, and more. Learn how to optimize performance, configure transport settings, correctly handle return codes, and leverage HTTP features effectively. Uncover techniques for consuming Web API in iOS and Android, as well as best practices for developing and testing in various platforms. Get tips for successful integration and persistence, and explore demo questions and contact details for additional support.

holden
Télécharger la présentation

Mastering Web API for Mobile: Best Practices & Implementation Tips

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Web API for Mobile • JaxARCSIG April 2012

  2. About Me • David Fekke L.L.C. • Mobile apps for iOS • Regular presenter at JaxDUG, JSSUG and JaxFusion • Writing Web Services for over 10 years

  3. History of Web Services • 1989 - Tim Berners-Lee invents HTTP/HTML • 1998 - XML 1.0, SOAP begins ratification • 2001 - SOAP standard • 2000 - Fielding dissertation on REST

  4. SOAP • Simple Object Access Protocol • Uses a standard XML Schema over HTTP • Extremely cross platform compatible • Extremely Slow

  5. REST • Representable State Transfer • Uses standard HTTP • Can use any text format including XML

  6. XML vs JSON • XML tag based document formatting • Javascript Notation by Douglas Crockford • JSON less verbose than XML, more lightweight • Mobile devices have limited bandwidth

  7. Public APIs • Twitter • Facebook • Flickr • Amazon • iTunes

  8. WebAPI • Available now as Nuget Package • Built-in as part of MVC 4 • Take advantage of HTTP features directly

  9. HTTP methods as Actions • Default route will use http method for action • Controller/action/id • API/Controller/id GET/POST/PUT/DELETE

  10. HTTP Method meanings • Get - Return an existing document • Post - Create a new document • Put - Update a document • Delete - Self explanatory

  11. Configure Transport • Set Xml or JSON based on Content-Type or Accept header • Accept: application/xml • Can also use Odata

  12. Return Codes • Now have the ability to specify return codes beside 200 • HttpResponseMessage<YourEntity> • HttpStatusCode.Created 201 • response.Headers.Location = new Uri()

  13. Http Status codes • 201 Created • 200 Success/204 Success but No Content • 403 Not authorized • 404 Does not exist • 500 Server Error • 301 Uri Moved

  14. Security • [Authorize()] • https over port 443 • Security Tokens • OAuth

  15. Testing WebAPI • Download Fiddler2 • Firebug (Firefox) • Chrome • On Mac use CocoaRestClient

  16. Consuming WebAPI • Web Apps (ASP.NET, MVC, PHP, Java, ColdFusion, Ruby(Rails), Python, Perl(if you are masochistic)) • JavaScript/JQuery • Mobile (iOS, Android, WP7, Blackberry OS)

  17. Develop WebAPI and iOS on Same Computer • Parallels or VMWare • Set Network Adapter to Bridged • Run Visual Studio as Administrator • Host on IIS (do not use IIS Express or Casini)

  18. Consuming WebAPI in iOS • Use NSURLConnection delegate or GCD • Show progress while waiting on response • Use JSON over XML • NSJSONSerialization class (new in iOS 5)

  19. XML Parsing in iOS • NSXMLParser (Slowest) • libxml2 (C api) • TBXML (DOM, Fastest, no write or xpath) • TouchXML (DOM supports xpath, no write) • KissXML (based on Touch, can write) • GDataXML (DOM, from Google) • RaptureXML (DOM, supports xpath)

  20. Call WebAPI from Android • Create DefaultHttpClient(); • Create request with HttpGet(Url); • Create response handler BasicResponseHandler(); • httpClient.execute(request, handler);

  21. JSON in Android • use the JSONObject to parse • JSONObject jo = new JSONObject(jString); • jo.getJSONObject(“car”); • jo.getJSONArray(“cars”);

  22. XML Parsing in Android • DOM, SAX and Pull • W3C Dom parser • Standard Java Sax Parser • SJXP (Pull parser)

  23. WebAPI as persistence • Don’t use WebAPI as default persistence on Mobile • Both Android and iOS have device persistence • local storage, CoreData and SQLite • iCloud to sync between iOS devices

  24. Demo

  25. Questions

  26. Contact Info • David Fekke • fekke.com/blog • david fekke at gmail dot com • twitter @davidfekke • aim: davefekke

More Related