Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length:...

Post on 27-Aug-2020

5 views 0 download

Transcript of Developing Android REST Client Applications · User-Agent: JavaDay participant Content-Length:...

Developing Android REST ClientApplications

Tomche Delev

Java User GroupMacedoniawww.jug.mk

December 18, 2011

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

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

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

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

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

The Incorrect Implementation of RESTMethos

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

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

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

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

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

Implementing REST Methods using aService API

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

Implementing REST Methods using aService API

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

The Processor (POST & PUT)

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

The Processor (DELTE & GET)

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

Implementing REST Methods using aService API

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

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

Implementing REST Methods using aService API

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

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

Implementing REST Methods using aService API

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

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

Implementing REST Methods using aService API

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

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

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

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