android-developing-restful-android-apps.pdf

56

Upload: rogzjrbernz

Post on 28-Nov-2015

46 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: android-developing-RESTful-android-apps.pdf
Page 2: android-developing-RESTful-android-apps.pdf

Developing Android REST Client ApplicationsVirgil Dobjanschi5/20/2010

Page 3: android-developing-RESTful-android-apps.pdf

Developing Android REST Client Applications

• View live notes and ask questions about this session on Google Wave:

–http://bit.ly/bHNnTm

3

Page 4: android-developing-RESTful-android-apps.pdf

REST Client Applications

4

Page 5: android-developing-RESTful-android-apps.pdf

REST Client Applications

• REST: A broadly adopted architecture style

4

Page 6: android-developing-RESTful-android-apps.pdf

REST Client Applications

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

4

Page 7: android-developing-RESTful-android-apps.pdf

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

Page 8: android-developing-RESTful-android-apps.pdf

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

Page 9: android-developing-RESTful-android-apps.pdf

The Incorrect Implementation of REST Methods

6

Activity

REST Method

Worker thread

Processor

Memory Storage

1. Get, create, update, delete

2. GET/POST/PUT/DELETE

3. Process data

4. Save data structure

CursorAdapter

Page 10: android-developing-RESTful-android-apps.pdf

What’s wrong with this approach?

7

Page 11: android-developing-RESTful-android-apps.pdf

What’s wrong with this approach?

• The operating system may shut down the process

7

Page 12: android-developing-RESTful-android-apps.pdf

What’s wrong with this approach?

• The operating system may shut down the process• Data is not persistently stored

7

Page 13: android-developing-RESTful-android-apps.pdf

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

Page 14: android-developing-RESTful-android-apps.pdf

REST Method Implementation Patterns

9

Page 15: android-developing-RESTful-android-apps.pdf

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

Page 16: android-developing-RESTful-android-apps.pdf

Implementing REST MethodsOption A: Use a Service API

Page 17: android-developing-RESTful-android-apps.pdf

Option A: Use a Service API

11

Activity

Service Helper

Service

REST Method

Content ProviderProcessor

1. initiate(param)

2. startService(Intent)

5. start(param)

6. GET/POST/PUT/DELETE

7. REST method complete callback

10. Binder callback

11. Callback to registered activities

8”. requery8’. ContentObserver notification

CursorAdapter

3. start(param) 9. Operation complete callback4.insert/update

8.insert/update

Page 18: android-developing-RESTful-android-apps.pdf

The REST Method

12

REST Method

Page 19: android-developing-RESTful-android-apps.pdf

The REST Method

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

12

REST Method

Page 20: android-developing-RESTful-android-apps.pdf

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

Page 21: android-developing-RESTful-android-apps.pdf

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)

• Enable the gzip content encoding when possible

12

REST Method

Page 22: android-developing-RESTful-android-apps.pdf

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)

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

12

REST Method

Page 23: android-developing-RESTful-android-apps.pdf

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)

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

12

REST Method

Page 24: android-developing-RESTful-android-apps.pdf

Option A: Use a Service API

13

Activity

Service Helper

Service

REST Method

Content ProviderProcessor

1. initiate(param)

2. startService(Intent)

5. start(param)

6. GET/POST/PUT/DELETE

7. REST method complete callback

10. Binder callback

11. Callback to registered activities

8”. requery8’. ContentObserver notification

CursorAdapter

3. start(param) 9. Operation complete callback4.insert/update

8.insert/update

Page 25: android-developing-RESTful-android-apps.pdf

The Processor (POST & PUT)

14

Content ProviderProcessor

4.insert(set STATE_POSTING)

8.update (clear STATE_POSTING)

Processor

POST

PUT

Content ProviderProcessor

4.update(set STATE_UPDATING)

8.update (clear STATE_UPDATING)

REST Method

REST Method

Page 26: android-developing-RESTful-android-apps.pdf

The Processor (DELETE & GET)

15

Content ProviderProcessor

4.update(set STATE_DELETING)

8.delete

Processor

DELETE

GET

Content ProviderProcessor8.insert new resources

REST Method

REST Method

Page 27: android-developing-RESTful-android-apps.pdf

Option A: Use a Service API

16

Activity

Service Helper

Service

REST Method

Content ProviderProcessor

1. initiate(param)

2. startService(Intent)

5. start(param)

6. GET/POST/PUT/DELETE

7. REST method complete callback

10. Binder callback

11. Callback to registered activities

8”. requery8’. ContentObserver notification

CursorAdapter

3. start(param) 9. Operation complete callback4.insert/update

8.insert/update

Page 28: android-developing-RESTful-android-apps.pdf

The Service

17

Service

Page 29: android-developing-RESTful-android-apps.pdf

The Service

• The role of the service

17

Service

Page 30: android-developing-RESTful-android-apps.pdf

The Service

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

17

Service

Page 31: android-developing-RESTful-android-apps.pdf

The 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

Service

Page 32: android-developing-RESTful-android-apps.pdf

The 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

Service

Page 33: android-developing-RESTful-android-apps.pdf

Option A: Use a Service API

18

Activity

Service Helper

Service

REST Method

Content ProviderProcessor

1. initiate(param)

2. startService(Intent)

5. start(param)

6. GET/POST/PUT/DELETE

7. REST method complete callback

10. Binder callback

11. Callback to registered activities

8”. requery8’. ContentObserver notification

CursorAdapter

3. start(param) 9. Operation complete callback4.insert/update

8.insert/update

Page 34: android-developing-RESTful-android-apps.pdf

The Service Helper

19

Service Helper

Page 35: android-developing-RESTful-android-apps.pdf

The Service Helper

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

19

Service Helper

Page 36: android-developing-RESTful-android-apps.pdf

The 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

Service Helper

Page 37: android-developing-RESTful-android-apps.pdf

The 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

Service Helper

Page 38: android-developing-RESTful-android-apps.pdf

Option A: Use a Service API

20

Activity

Service Helper

Service

REST Method

Content ProviderProcessor

1. initiate(param)

2. startService(Intent)

5. start(param)

6. GET/POST/PUT/DELETE

7. REST method complete callback

10. Binder callback

11. Callback to registered activities

8”. requery8’. ContentObserver notification

CursorAdapter

3. start(param) 9. Operation complete callback4.insert/update

8.insert/update

Page 39: android-developing-RESTful-android-apps.pdf

Handling the REST Method in anActivity

21

Activity & CursorAdapter

Page 40: android-developing-RESTful-android-apps.pdf

Handling the REST Method in anActivity

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

21

Activity & CursorAdapter

Page 41: android-developing-RESTful-android-apps.pdf

Handling the REST Method in anActivity

• 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

Activity & CursorAdapter

Page 42: android-developing-RESTful-android-apps.pdf

Handling the REST Method in anActivity

• 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

Activity & CursorAdapter

Page 43: android-developing-RESTful-android-apps.pdf

Option A: Use a Service API

22

Activity

Service Helper

Service

REST Method

Content ProviderProcessor

1. initiate(param)

2. startService(Intent)

5. start(param)

6. GET/POST/PUT/DELETE

7. REST method complete callback

10. Binder callback

11. Callback to registered activities

8”. requery8’. ContentObserver notification

CursorAdapter

3. start(param) 9. Operation complete callback4.insert/update

8.insert/update

Page 44: android-developing-RESTful-android-apps.pdf

Implementing REST MethodsOption B: Use the ContentProvider API

Page 45: android-developing-RESTful-android-apps.pdf

Option B: Use the ContentProvider API

24

Activity & CursorAdapter

Service

REST Method

Content Provider

Processor

1. query, insert, update, delete

3. startService(Intent)

4. start(param)

7’. ContentObserver notification 7”. query

7. insert/update

5. GET/POST/PUT/DELETE

6. Process data

Service Helper

2. start(Intent)

Page 46: android-developing-RESTful-android-apps.pdf

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

Page 47: android-developing-RESTful-android-apps.pdf

A Simple Pattern Using the ContentProvider API

26

Sync Adapter

REST Method

Content Provider

Processor

2. start(param)

6’. requery

4. Process data

6. ContentObserver notification

Activity & CursorAdapter

query, insert, update, delete

1.Get items that need to be synced

Use a sync adapter to initiate all your REST methods

5. insert/update/delete

3. GET/POST/PUT/DELETE

Page 48: android-developing-RESTful-android-apps.pdf

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

Page 49: android-developing-RESTful-android-apps.pdf

Conclusions

28

Page 50: android-developing-RESTful-android-apps.pdf

Conclusions

• Do not implement REST methods inside Activities

28

Page 51: android-developing-RESTful-android-apps.pdf

Conclusions

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

28

Page 52: android-developing-RESTful-android-apps.pdf

Conclusions

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

28

Page 53: android-developing-RESTful-android-apps.pdf

Conclusions

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

28

Page 54: android-developing-RESTful-android-apps.pdf

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

Page 55: android-developing-RESTful-android-apps.pdf

Developing Android REST Client Applications

• View live notes and ask questions about this session on Google Wave:

–http://bit.ly/bHNnTm

29

Page 56: android-developing-RESTful-android-apps.pdf