Developing Android REST Client Applications Virgil Dobjanschi 5/20/2010

Developing Android REST Client Applications • View live notes and ask questions about this session on Google Wave:

–http://bit.ly/bHNnTm

3

REST Client Applications

4

REST Client Applications • REST: A broadly adopted architecture style

4

REST Client Applications • REST: A broadly adopted architecture style • A large number of REST APIs are available

4

REST Client Applications • REST: A broadly adopted architecture style • A large number of REST APIs are available • Why develop them if mobile friendly web sites already exist?

4

Incorrect Implementation of REST Methods “I haven’t failed, I’ve found 10,000 ways that don’t work.” - Thomas Alva Edison

The Incorrect Implementation of REST Methods 1. Get, create, update, delete

Activity

CursorAdapter

Worker thread REST Method 2. GET/POST/ PUT/DELETE

3. Process data

Processor

4. Save data structure

Memory Storage 6

What’s wrong with this approach?

7

What’s wrong with this approach? • The operating system may shut down the process

7

What’s wrong with this approach? • The operating system may shut down the process • Data is not persistently stored

7

Implementing REST Methods “There’s a way to do it better ... find it.” - Thomas Alva Edison

REST Method Implementation Patterns

9

REST Method Implementation Patterns • Introducing three design patterns to handle REST methods – Use a Service API – Use the ContentProvider API – Use the ContentProvider API and a SyncAdapter

9

Implementing REST Methods Option A: Use a Service API

Option A: Use a Service API Activity 1. initiate(param)

CursorAdapter

11. Callback to registered activities

Service Helper 2. startService(Intent)

8’. ContentObserver notification

8”. requery

10. Binder callback

Service 3. start(param)

9. Operation complete callback 4.insert/update

Processor 5. start(param)

8.insert/update 7. REST method complete callback

REST Method 11

Content Provider

6. GET/POST/PUT/DELETE

The REST Method

12

REST Method

The REST Method • An entity which: – Prepares the HTTP URL & HTTP request body – Executes the HTTP transaction – Processes the HTTP response

12

REST Method

The REST Method • An entity which: – Prepares the HTTP URL & HTTP request body – Executes the HTTP transaction – Processes the HTTP response

• Select the optimal content type for responses – Binary, JSON, XML – New in Froyo: JSON parser (same org.json API)

12

REST Method

The REST Method

REST Method

• An entity which: – Prepares the HTTP URL & HTTP request body – Executes the HTTP transaction – Processes the HTTP response

• Select the optimal content type for responses – Binary, JSON, XML – New in Froyo: JSON parser (same org.json API)

• Enable the gzip content encoding when possible

12

The REST Method

REST Method

• An entity which: – Prepares the HTTP URL & HTTP request body – Executes the HTTP transaction – Processes the HTTP response

• Select the optimal content type for responses – Binary, JSON, XML – New in Froyo: JSON parser (same org.json API)

• Enable the gzip content encoding when possible • Run the REST method in a worker thread

12

The REST Method

REST Method

• An entity which: – Prepares the HTTP URL & HTTP request body – Executes the HTTP transaction – Processes the HTTP response

• Select the optimal content type for responses – Binary, JSON, XML – New in Froyo: JSON parser (same org.json API)

• Enable the gzip content encoding when possible • Run the REST method in a worker thread • Use the Apache HTTP client

12

Option A: Use a Service API Activity 1. initiate(param)

CursorAdapter

11. Callback to registered activities

Service Helper 2. startService(Intent)

8’. ContentObserver notification

8”. requery

10. Binder callback

Service 3. start(param)

9. Operation complete callback 4.insert/update

Processor 5. start(param)

8.insert/update 7. REST method complete callback

REST Method 13

Content Provider

6. GET/POST/PUT/DELETE

The Processor (POST & PUT)

Processor

POST Processor

4.insert (set STATE_POSTING) 8.update (clear STATE_POSTING)

Content Provider

REST Method PUT Processor REST Method 14

4.update (set STATE_UPDATING) 8.update (clear STATE_UPDATING)

Content Provider

The Processor (DELETE & GET)

Processor

DELETE Processor

4.update (set STATE_DELETING)

Content Provider

8.delete

REST Method GET Processor REST Method 15

8.insert new resources

Content Provider

Option A: Use a Service API Activity 1. initiate(param)

CursorAdapter

11. Callback to registered activities

Service Helper 2. startService(Intent)

8’. ContentObserver notification

8”. requery

10. Binder callback

Service 3. start(param)

9. Operation complete callback 4.insert/update

Processor 5. start(param)

8.insert/update 7. REST method complete callback

REST Method 16

Content Provider

6. GET/POST/PUT/DELETE

The Service

17

Service

The Service • The role of the service

17

Service

The Service

Service

• The role of the service • Forward path: receives the Intent sent by the Service Helper and starts the corresponding REST Method

17

The Service

Service

• The role of the service • Forward path: receives the Intent sent by the Service Helper and starts the corresponding REST Method • Return path: handles the Processor callback and invokes the Service Helper binder callback

17

The Service

Service

• The role of the service • Forward path: receives the Intent sent by the Service Helper and starts the corresponding REST Method • Return path: handles the Processor callback and invokes the Service Helper binder callback • It can implement a queue of downloads

17

Option A: Use a Service API Activity 1. initiate(param)

CursorAdapter

11. Callback to registered activities

Service Helper 2. startService(Intent)

8’. ContentObserver notification

8”. requery

10. Binder callback

Service 3. start(param)

9. Operation complete callback 4.insert/update

Processor 5. start(param)

8.insert/update 7. REST method complete callback

REST Method 18

Content Provider

6. GET/POST/PUT/DELETE

The Service Helper

19

Service Helper

The Service Helper

Service Helper

• Singleton which exposes a simple asynchronous API to be used by the user interface

19

The Service Helper

Service Helper

• Singleton which exposes a simple asynchronous API to be used by the user interface • Prepare and send the Service request – Check if the method is already pending – Create the request Intent – Add the operation type and a unique request id – Add the method specific parameters – Add the binder callback – Call startService(Intent) – Return the request id

19

The Service Helper

Service Helper

• Singleton which exposes a simple asynchronous API to be used by the user interface • Prepare and send the Service request – Check if the method is already pending – Create the request Intent – Add the operation type and a unique request id – Add the method specific parameters – Add the binder callback – Call startService(Intent) – Return the request id

• Handle the callback from the service – Dispatch callbacks to the user interface listeners 19

Option A: Use a Service API Activity 1. initiate(param)

CursorAdapter

11. Callback to registered activities

Service Helper 2. startService(Intent)

8’. ContentObserver notification

8”. requery

10. Binder callback

Service 3. start(param)

9. Operation complete callback 4.insert/update

Processor 5. start(param)

8.insert/update 7. REST method complete callback

REST Method 20

Content Provider

6. GET/POST/PUT/DELETE

Handling the REST Method in an Activity

21

Activity & CursorAdapter

Handling the REST Method in an Activity

Activity & CursorAdapter

• Add an operation listener in onResume and remove it in onPause

21

Handling the REST Method in an Activity

Activity & CursorAdapter

• Add an operation listener in onResume and remove it in onPause • Consider these cases: – The Activity is still active when the request completes – The Activity is paused then resumed and then the request completes – The Activity is paused when the request completes and then Activity is resumed

21

Handling the REST Method in an Activity

Activity & CursorAdapter

• Add an operation listener in onResume and remove it in onPause • Consider these cases: – The Activity is still active when the request completes – The Activity is paused then resumed and then the request completes – The Activity is paused when the request completes and then Activity is resumed

• The CursorAdapter handles the ContentProvider notification by implementing a ContentObserver

21

Option A: Use a Service API Activity 1. initiate(param)

CursorAdapter

11. Callback to registered activities

Service Helper 2. startService(Intent)

8’. ContentObserver notification

8”. requery

10. Binder callback

Service 3. start(param)

9. Operation complete callback 4.insert/update

Processor 5. start(param)

8.insert/update 7. REST method complete callback

REST Method 22

Content Provider

6. GET/POST/PUT/DELETE

Implementing REST Methods Option B: Use the ContentProvider API

Option B: Use the ContentProvider API Activity & CursorAdapter 1. query, insert, update, delete

7’. ContentObserver notification

7”. query

Content Provider 2. start(Intent)

Service Helper 3. startService(Intent) 7. insert/update

Service 4. start(param)

REST Method 24

5. GET/POST/ PUT/DELETE

6. Process data

Processor

A Simple Pattern for the REST of us Option C: Use a ContentProvider API and a SyncAdapter “To have a great idea, have a lot of them.” -Thomas Alva Edison

A Simple Pattern Using the ContentProvider API Use a sync adapter to initiate all your REST methods

Activity & CursorAdapter query, insert, update, delete

6. ContentObserver notification

Content Provider 1.Get items that need to be synced 5. insert/ update/delete

Sync Adapter 2. start(param)

REST Method 3. GET/POST/ PUT/DELETE

26

4. Process data

Processor

6’. requery

Conclusions “The value of an idea lies in the using of it.” - Thomas Alva Edison

Conclusions

28

Conclusions • Do not implement REST methods inside Activities

28

Conclusions • Do not implement REST methods inside Activities • Start long running operations from a Service

28

Conclusions • Do not implement REST methods inside Activities • Start long running operations from a Service • Persist early & persist often

28

Conclusions • Do not implement REST methods inside Activities • Start long running operations from a Service • Persist early & persist often • Minimize the network usage

28

Conclusions • Do not implement REST methods inside Activities • Start long running operations from a Service • Persist early & persist often • Minimize the network usage • Use a sync adapter to execute background operations which are not time critical – New in Froyo: Android Cloud to Device Messaging

28

Developing Android REST Client Applications • View live notes and ask questions about this session on Google Wave:

–http://bit.ly/bHNnTm

29

Quelle: Developing Android REST Client Applications dl

A description for this result is not available because of this site's robots.txt

1MB Sizes 2 Downloads 304 Views

Recommend Documents

Android UI Design Patterns dl
A description for this result is not available because of this site's robots.txtLearn more

Writing Zippy Android Apps dl
A description for this result is not available because of this site's robots.txtLearn more

Android Beginners Guide ( pdf ) dl
A description for this result is not available because of this site's robots.txtLearn more

Dobjanschi - Android REST transcript v03.pdf
Page 1 of 2. Stand 02/ 2000 MULTITESTER I Seite 1. RANGE MAX/MIN VoltSensor HOLD. MM 1-3. V. V. OFF. Hz A. A. °C. °F. Hz. A. MAX. 10A. FUSED.

Developing Global Applications in Java
Designing the program from the ground up with this kind of customization in .... application in such a way as to prevent translating it into other languages in the.

Building Push Applications for Android
Use the Intent API to get a registration ID. // Registration ID is compartmentalized per app/device. Intent regIntent = new. Intent(“com.google.android.c2dm.intent.REGISTER”);. // Identify your app. regIntent.putExtra(“app”,. PendingIntent.ge

developing ios applications nit-goa -
to confidently dive into iOS application development and catch up with the mobile revolution. ... center, localization, maps, touches and gestures and camera and ...

Developing Scientific Applications with Loosely-Coupled ... - GitHub
application developer to focus on supporting the application characteristics and ... Jobs. Dist. Application Patterns / Usage Modes. Distributed Applications.

ola dl - GitHub
o pleax movl 0 esi m ov l. 10 edx jmpreadarg, readarg readchmovb. 0 eax edi bl cmpl 0 ebx jest argimuledx ecx subl48 ebx a d dl ebx ecx sto reargmov.

DL Brochure.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. DL Brochure.pdf.

Learning RxJava - Ebook-dl
Did you know that Packt offers eBook versions of every book published, with PDF and. ePub files available? You can upgrade ...... Observable; import java.util.concurrent.TimeUnit; public class Launcher { public static void main(String[]args) {. Obser

Racine quelle place en ville.pdf
Racine quelle place en ville.pdf. Racine quelle place en ville.pdf. Open. Extract. Open with. Sign In. Main menu. Displaying Racine quelle place en ville.pdf.

Web dl 2005
Serious man pdf. Web dl 2005 -. Download.Web dl 2005.James patterson david ellis.Web dl 2005.Web dl 2005.Tinker tailor soldier spy anox.Acid bathwhen the kitestring. pops. The wires01 german.009698483.Dancing bear #11.Top itunes songs.Xara web site.S

Battery Life, That Is dl
A description for this result is not available because of this site's robots.txtLearn more

Albiway-DL-16_7_23.pdf
Connect more apps... Try one of the apps below to open or edit this item. Albiway-DL-16_7_23.pdf. Albiway-DL-16_7_23.pdf. Open. Extract. Open with. Sign In.