Android REST-client applications:       services approach         Sharing experience
Case
Case       New startup
Case       New startup
Case       New startup
Case       New startup
Case       New startup          stores          posts          sends         provides          notifies         suggests
Case       New startup          REST
RESThttps://api.twitter.com/1/users/show.json?screen_name=roman_mazur     {         id : 14701612,         name : “Roman M...
How to perform requests@Overridepublic void onClick(View view) {  URLConnection connection    = new URL(…).openConnection(...
How to perform requests@Overridepublic void onClick(View view) {  URLConnection connection    = new URL(…).openConnection(...
How to perform requests• Obviously: not in the main (GUI) thread• Using either URLConnection or HttpClient  – both have pr...
Why not to use activities?• User controls activity lifecycle• When all your activities are in background  your process is ...
Services Way• See also  – Google IO 2010 session    “Developing Android REST Client Applications”
Services: our first implementation                      Activity           1. onStart                 4. performRequest   ...
Services: our first implementation• Main problem: rotations :)  – we register listener in onStart and remove it in    onSt...
Current implementation• Loaders!  – are awesome since they are not recreated in case    of configuration changes (at least...
How we perform requests now@Overridepublic void onActivityCreated(Bundle savedInstanceState) {  super.onActivityCreated(sa...
How we perform requests now@Overridepublic Loader<ResponseData<Profile>>    onCreateLoader(int id, Bundle args) {    retur...
How we perform requests now@Overridepublic void    onLoadFinished(Loader<ResponseData<Profile>> loader,                   ...
How we perform requests now@Overridepublic void    onLoadFinished(Loader<ResponseData<Profile>> loader,                   ...
ResponseData•   Result code•   Status message•   User visible message•   Data
Activity Side• Request builder creates a request description• Description is passed to a service  – a) as an Intent  – b) ...
Service Side• Either enqueues description processing or  performs it in the worker thread using  AsyncTask• Request descri...
Service Side: Response Pipeline                       URLConnection   ContentHandler   Parsed object                    Re...
Conclusions• Power  – requests processing can be easily managed  – requests can triggered by notifications and    AlarmMan...
It’s open sourceBut we lack documentation :)http://code.google.com/p/enroscar/and we are preparing to release it on GitHub
Thanks!Roman MazurHead of Android Unit at StanfyKyiv GDD co-organizermazur.roman@gmail.com+Roman Mazur@roman_mazur
Upcoming SlideShare
Loading in …5
×

Android rest client applications-services approach @Droidcon Bucharest 2012

3,402 views

Published on

Presented by Roman Mazur

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,402
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
54
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Android rest client applications-services approach @Droidcon Bucharest 2012

  1. 1. Android REST-client applications: services approach Sharing experience
  2. 2. Case
  3. 3. Case New startup
  4. 4. Case New startup
  5. 5. Case New startup
  6. 6. Case New startup
  7. 7. Case New startup stores posts sends provides notifies suggests
  8. 8. Case New startup REST
  9. 9. RESThttps://api.twitter.com/1/users/show.json?screen_name=roman_mazur { id : 14701612, name : “Roman Mazur”, location : “Ukraine, Kyiv”, … }
  10. 10. How to perform requests@Overridepublic void onClick(View view) { URLConnection connection = new URL(…).openConnection(); …}
  11. 11. How to perform requests@Overridepublic void onClick(View view) { URLConnection connection = new URL(…).openConnection(); …}
  12. 12. How to perform requests• Obviously: not in the main (GUI) thread• Using either URLConnection or HttpClient – both have pros and cons• Choose context: Activity vs. Service
  13. 13. Why not to use activities?• User controls activity lifecycle• When all your activities are in background your process is a candidate for killing• You’ll lose your data
  14. 14. Services Way• See also – Google IO 2010 session “Developing Android REST Client Applications”
  15. 15. Services: our first implementation Activity 1. onStart 4. performRequest ApiMethodsSupport (Helper for communication with service) 3. registerListener 2. bind 5. performRequest (using binder) Service 6. HTTP GET/POST/…
  16. 16. Services: our first implementation• Main problem: rotations :) – we register listener in onStart and remove it in onStop – what if response is received while we are rotating the screen?
  17. 17. Current implementation• Loaders! – are awesome since they are not recreated in case of configuration changes (at least in most cases)• Custom loader – binds to the service – registers listener – performs request – gets the result – unbinds
  18. 18. How we perform requests now@Overridepublic void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); getLoaderManager().initLoader(1, null, this);}
  19. 19. How we perform requests now@Overridepublic Loader<ResponseData<Profile>> onCreateLoader(int id, Bundle args) { return new SimpleRequestBuilder<Profile>(getActivity()) { } .setUrl("https://api.twitter.com/1/users/show.json") .addParam("screen_name", "TwitterAPI") .getLoader();}
  20. 20. How we perform requests now@Overridepublic void onLoadFinished(Loader<ResponseData<Profile>> loader, ResponseData<Profile> data) { if (data.isSuccessful()) { Profile profile = data.getModel(); text1.setText(profile.getName()); text2.setText(profile.getDescription()); }}
  21. 21. How we perform requests now@Overridepublic void onLoadFinished(Loader<ResponseData<Profile>> loader, ResponseData<Profile> data) { if (data.isSuccessful()) { Profile profile = data.getModel(); text1.setText(profile.getName()); text2.setText(profile.getDescription()); }}
  22. 22. ResponseData• Result code• Status message• User visible message• Data
  23. 23. Activity Side• Request builder creates a request description• Description is passed to a service – a) as an Intent – b) with a service binder method performRequest
  24. 24. Service Side• Either enqueues description processing or performs it in the worker thread using AsyncTask• Request description builds URLConnection• Input thread is read, parsed; result is analyzed and then delivered to listeners
  25. 25. Service Side: Response Pipeline URLConnection ContentHandler Parsed object ResponseDataConverter ResponseData ContentAnalyzer Listeners Analyzed ResponseData
  26. 26. Conclusions• Power – requests processing can be easily managed – requests can triggered by notifications and AlarmManager• Simplicity – not much to learn if you are familiar with Android loaders• Caveats – currently not everything is easy to customize
  27. 27. It’s open sourceBut we lack documentation :)http://code.google.com/p/enroscar/and we are preparing to release it on GitHub
  28. 28. Thanks!Roman MazurHead of Android Unit at StanfyKyiv GDD co-organizermazur.roman@gmail.com+Roman Mazur@roman_mazur

×