Connecting to Web Services on Android

27
Connecting to Web Services Sean Sullivan Android Dev Camp Amsterdam 8 January 2009

description

This document gives you the needed resources to do webservice on Android

Transcript of Connecting to Web Services on Android

Page 1: Connecting to Web Services on Android

Connecting to Web Services

Sean SullivanAndroid Dev Camp Amsterdam

8 January 2009

Page 2: Connecting to Web Services on Android
Page 3: Connecting to Web Services on Android

+

Page 4: Connecting to Web Services on Android
Page 5: Connecting to Web Services on Android
Page 6: Connecting to Web Services on Android

• HTTP

• XML

• JSON

• OAuth

Page 7: Connecting to Web Services on Android

XML response

HTTP request

Page 9: Connecting to Web Services on Android

jfireeagle uses:

• HttpClient API

• XStream

• OAuth Java library

Page 10: Connecting to Web Services on Android

“An open protocol to allow secure API authorization in a simple and standard method from desktop and web applications”

Page 11: Connecting to Web Services on Android

• use OAuth to access protected data

• your application needs to obtain an access token from the service provider

• service provider will issue access token after the user grants permission

Page 12: Connecting to Web Services on Android

http://code.google.com/p/oauth

+

Page 13: Connecting to Web Services on Android

jfireeagle

http://code.google.com/p/jfireeagle/wiki/Android

jpoco

http://code.google.com/p/jpoco/wiki/Android

+

Page 14: Connecting to Web Services on Android

+ HTTP

Option 1

• java.net.URL

• java.net.HttpURLConnection

Option 2

• HttpClient 4.0 API

Page 15: Connecting to Web Services on Android

HttpClient 4.0 API

• org.apache.http.client.HttpClient

• org.apache.http.impl.client.DefaultHttpClient

Page 16: Connecting to Web Services on Android

HttpClient 4.0 API

• org.apache.http.client.methods.HttpGet

• org.apache.http.client.methods.HttpPost

• org.apache.http.client.methods.HttpPut

• org.apache.http.client.methods.HttpDelete

Page 17: Connecting to Web Services on Android

HttpClient 4.0 API

• org.apache.http.HttpResponse

• org.apache.http.StatusLine

• org.apache.http.HttpEntity

• org.apache.http.utils.EntityUtils

Page 18: Connecting to Web Services on Android

HttpClient programming

• HTTP response status code

• I/O exceptions

• timeouts

• gzip?

Page 19: Connecting to Web Services on Android

HttpClient programming

synchronous invocation public HttpResponse execute(HttpUriRequest request)

asynchronous invocation public HttpResponse execute(HttpUriRequest request,

ResponseHandler handler)

Page 20: Connecting to Web Services on Android

Android UI thread

• user clicks on button

• onClick listener

• Don’t block the UI thread!

• Tip: use java.lang.Runnable

Page 21: Connecting to Web Services on Android

Response formats

• XML

• JSON

• RSS, Atom

• other

Page 22: Connecting to Web Services on Android

XML processing

• org.w3c.dom package (DOM)

• org.xml.sax package (SAX)

• org.xmlpull package (XPP3)

• XStream

• JDOM

• Apache XmlBeans ** does not work on Android 1.0

Page 23: Connecting to Web Services on Android

JSON processing

• org.json package

• Google GSON library *

• XStream + Jettison *

• json-lib *

* does not work on Android 1.0

Page 24: Connecting to Web Services on Android

Client libraries

• code.google.com/p/jfireeagle

• code.google.com/p/jpoco

• code.google.com/p/meetup-java-client

• code.google.com/p/upcoming-java-client

• code.google.com/p/gdata-java-client

• flickrj.sourceforge.net

Page 25: Connecting to Web Services on Android

Conclusion

• HttpClient API

• use XStream for XML processing

• use org.json package for JSON processing

• OAuth has a learning curve

• OAuth affects user experience

Page 26: Connecting to Web Services on Android

Thank you

Page 27: Connecting to Web Services on Android

Resources

• http://code.google.com/android/documentation.html

• http://hc.apache.org/httpcomponents-client

• http://code.google.com/p/oauth

• http://xstream.codehaus.org