25
Developing Android REST Client Applications Tomche Delev Java User Group Macedonia www.jug.mk December 18, 2011

Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Developing Android REST ClientApplications

Tomche Delev

Java User GroupMacedoniawww.jug.mk

December 18, 2011

Page 2: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

REST Client Applications

REST: A broadly adopted architecture style

A large number of REST APIs are available

Why develop them if mobile friendly web sitesalreadyexist?

Tomche Delev (JUGMK) Android REST Applications December 2010 2 / 20

Page 3: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

REST Client Applications

REST: A broadly adopted architecture style

A large number of REST APIs are available

Why develop them if mobile friendly web sitesalreadyexist?

Tomche Delev (JUGMK) Android REST Applications December 2010 2 / 20

Page 4: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

REST Client Applications

REST: A broadly adopted architecture style

A large number of REST APIs are available

Why develop them if mobile friendly web sitesalreadyexist?

Tomche Delev (JUGMK) Android REST Applications December 2010 2 / 20

Page 5: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

REST Client Applications

REST: A broadly adopted architecture style

A large number of REST APIs are available

Why develop them if mobile friendly web sitesalreadyexist?

Tomche Delev (JUGMK) Android REST Applications December 2010 2 / 20

Page 6: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Incorrect Implementation of RESTMethods

”I haven’t failed, I’ve found 10,000 waysthat don’t work.”

- Thomas Alva Edison

Tomche Delev (JUGMK) Android REST Applications December 2010 3 / 20

Page 7: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

The Incorrect Implementation of RESTMethos

Tomche Delev (JUGMK) Android REST Applications December 2010 4 / 20

Page 8: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Whats wrong with this approach?

The operating system may shut down the process

Data is not persistently stored

Tomche Delev (JUGMK) Android REST Applications December 2010 5 / 20

Page 9: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Whats wrong with this approach?

The operating system may shut down the process

Data is not persistently stored

Tomche Delev (JUGMK) Android REST Applications December 2010 5 / 20

Page 10: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Whats wrong with this approach?

The operating system may shut down the process

Data is not persistently stored

Tomche Delev (JUGMK) Android REST Applications December 2010 5 / 20

Page 11: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Implementing REST Methods

Theres a way to do it better ... find it.

- Thomas Alva Edison

Tomche Delev (JUGMK) Android REST Applications December 2010 6 / 20

Page 12: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Implementing REST Methods using aService API

Tomche Delev (JUGMK) Android REST Applications December 2010 7 / 20

Page 13: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Implementing REST Methods using aService API

Tomche Delev (JUGMK) Android REST Applications December 2010 8 / 20

Page 14: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

The Processor (POST & PUT)

Tomche Delev (JUGMK) Android REST Applications December 2010 9 / 20

Page 15: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

The Processor (DELTE & GET)

Tomche Delev (JUGMK) Android REST Applications December 2010 10 / 20

Page 16: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Implementing REST Methods using aService API

Tomche Delev (JUGMK) Android REST Applications December 2010 11 / 20

Page 17: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

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 andinvokes the Service Helper binder callback

It can implement a queue of downloads

Tomche Delev (JUGMK) Android REST Applications December 2010 12 / 20

Page 18: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Implementing REST Methods using aService API

Tomche Delev (JUGMK) Android REST Applications December 2010 13 / 20

Page 19: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

The Service Helper

Singleton which exposes a simple asynchronous APIto be used by the user interfacePrepare and send the Service request

Check if the method is already pendingCreate the request IntentAdd the operation type and a unique request idAdd the method specific parametersAdd the binder callbackCall startService(Intent)

Return the request idHandle the callback from the service

Dispatch callbacks to the user interface listeners

Tomche Delev (JUGMK) Android REST Applications December 2010 14 / 20

Page 20: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Implementing REST Methods using aService API

Tomche Delev (JUGMK) Android REST Applications December 2010 15 / 20

Page 21: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Handling the REST Method in an Activity

Add an operation listener in onResume and removeit in onPauseConsider these cases:

The Activity is still active when the request completesThe Activity is paused then resumed and then therequest completesThe Activity is paused when the request completes andthen Activity is resumed

The CursorAdapter handles the ContentProvidernotification by implementing a ContentObserver

Tomche Delev (JUGMK) Android REST Applications December 2010 16 / 20

Page 22: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Implementing REST Methods using aService API

Tomche Delev (JUGMK) Android REST Applications December 2010 17 / 20

Page 23: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Conclusions

”To have a great idea, have a lot of them.”&”The value of an idea lies in the using of it.”

- Thomas Alva Edison

Tomche Delev (JUGMK) Android REST Applications December 2010 18 / 20

Page 24: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Conclusions

Do not implement REST methods inside Activities

Start long running operations from a Service

Persist early & persist often

Minimize the network usage

Tomche Delev (JUGMK) Android REST Applications December 2010 19 / 20

Page 25: Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length: reasonable Content-Type: android-related/java Tomche Delev (JUGMK) Android REST Applications

Questions

Thanks for listenting.Questions?POST questions HTTP/1.1Host: jug.mkUser-Agent: JavaDay participantContent-Length: reasonableContent-Type: android-related/java

Tomche Delev (JUGMK) Android REST Applications December 2010 20 / 20