web api for mobile jaxarcsig april 2012. about me david fekke l.l.c. mobile apps for ios regular...

27
Web API for Mobile JaxARCSIG April 2012

Upload: adelia-miller

Post on 18-Dec-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

Web API for MobileJaxARCSIG April 2012

Page 2: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

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

Page 3: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web
Page 4: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

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

Page 5: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

SOAP

Simple Object Access Protocol

Uses a standard XML Schema over HTTP

Extremely cross platform compatible

Extremely Slow

Page 6: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

REST

Representable State Transfer

Uses standard HTTP

Can use any text format including XML

Page 7: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

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

Page 8: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

Public APIs

Twitter

Facebook

Flickr

Amazon

iTunes

Page 9: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

WebAPI

Available now as Nuget Package

Built-in as part of MVC 4

Take advantage of HTTP features directly

Page 10: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

HTTP methods as Actions

Default route will use http method for action

Controller/action/id

API/Controller/id GET/POST/PUT/DELETE

Page 11: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

HTTP Method meanings

Get - Return an existing document

Post - Create a new document

Put - Update a document

Delete - Self explanatory

Page 12: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

Configure Transport

Set Xml or JSON based on Content-Type or Accept header

Accept: application/xml

Can also use Odata

Page 13: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

Return Codes

Now have the ability to specify return codes beside 200

HttpResponseMessage<YourEntity>

HttpStatusCode.Created 201

response.Headers.Location = new Uri()

Page 14: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

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

Page 15: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

Security

[Authorize()]

https over port 443

Security Tokens

OAuth

Page 16: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

Testing WebAPI

Download Fiddler2

Firebug (Firefox)

Chrome

On Mac use CocoaRestClient

Page 17: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

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)

Page 18: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

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)

Page 19: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

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)

Page 20: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

XML Parsing in iOSNSXMLParser (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)

Page 21: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

Call WebAPI from Android

Create DefaultHttpClient();

Create request with HttpGet(Url);

Create response handler BasicResponseHandler();

httpClient.execute(request, handler);

Page 22: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

JSON in Android

use the JSONObject to parse

JSONObject jo = new JSONObject(jString);

jo.getJSONObject(“car”);

jo.getJSONArray(“cars”);

Page 23: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

XML Parsing in Android

DOM, SAX and Pull

W3C Dom parser

Standard Java Sax Parser

SJXP (Pull parser)

Page 24: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

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

Page 25: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

Demo

Page 26: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

Questions

Page 27: Web API for Mobile JaxARCSIG April 2012. About Me David Fekke L.L.C. Mobile apps for iOS Regular presenter at JaxDUG, JSSUG and JaxFusion Writing Web

Contact Info

David Fekke

fekke.com/blog

david fekke at gmail dot com

twitter @davidfekke

aim: davefekke