SlideShare une entreprise Scribd logo
1  sur  28
Télécharger pour lire hors ligne
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
REST
https://api.twitter.com/1/users/show.json?screen_name=roman_mazur



     {
         id : 14701612,
         name : “Roman Mazur”,
         location : “Ukraine, Kyiv”,
         …
     }
How to perform requests
@Override
public void onClick(View view) {
  URLConnection connection
    = new URL(…).openConnection();
  …
}
How to perform requests
@Override
public 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 pros and cons


• Choose context: Activity vs. Service
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
Services Way
• See also
  – Google IO 2010 session
    “Developing Android REST Client Applications”
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/…
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?
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
How we perform requests now


@Override
public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);

    getLoaderManager().initLoader(1, null, this);

}
How we perform requests now
@Override
public 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();

}
How we perform requests now
@Override
public void
    onLoadFinished(Loader<ResponseData<Profile>> loader,
                   ResponseData<Profile> data) {

    if (data.isSuccessful()) {
      Profile profile = data.getModel();
      text1.setText(profile.getName());
      text2.setText(profile.getDescription());
    }

}
How we perform requests now
@Override
public void
    onLoadFinished(Loader<ResponseData<Profile>> loader,
                   ResponseData<Profile> data) {

    if (data.isSuccessful()) {
      Profile profile = data.getModel();
      text1.setText(profile.getName());
      text2.setText(profile.getDescription());
    }

}
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) with a service binder method
    performRequest
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
Service Side: Response Pipeline
                       URLConnection



   ContentHandler


   Parsed object


                    ResponseDataConverter


                                 ResponseData


                       ContentAnalyzer                         Listeners
                                                  Analyzed
                                                ResponseData
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
It’s open source
But we lack documentation :)




http://code.google.com/p/enroscar/
and we are preparing to release it on GitHub
Thanks!
Roman Mazur

Head of Android Unit at Stanfy
Kyiv GDD co-organizer




mazur.roman@gmail.com
+Roman Mazur
@roman_mazur

Contenu connexe

Tendances

Learning React - I
Learning React - ILearning React - I
Learning React - IMitch Chen
 
Introduce Flux & react in practices (KKBOX)
Introduce Flux & react in practices (KKBOX)Introduce Flux & react in practices (KKBOX)
Introduce Flux & react in practices (KKBOX)Hsuan Fu Lien
 
3rd Generation Web Application Platforms
3rd Generation Web Application Platforms3rd Generation Web Application Platforms
3rd Generation Web Application PlatformsNaresh Chintalcheru
 
Introduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITIntroduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITnamespaceit
 
Breaking the Server-Client Divide with Node.js and React
Breaking the Server-Client Divide with Node.js and ReactBreaking the Server-Client Divide with Node.js and React
Breaking the Server-Client Divide with Node.js and ReactDejan Glozic
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners Varun Raj
 
Simple REST with Dropwizard
Simple REST with DropwizardSimple REST with Dropwizard
Simple REST with DropwizardAndrei Savu
 
Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...
Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...
Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...DroidConTLV
 
Akka - Developing SEDA Based Applications
Akka - Developing SEDA Based ApplicationsAkka - Developing SEDA Based Applications
Akka - Developing SEDA Based ApplicationsBenjamin Darfler
 
The Past Year in Spring for Apache Geode
The Past Year in Spring for Apache GeodeThe Past Year in Spring for Apache Geode
The Past Year in Spring for Apache GeodeVMware Tanzu
 
Enterprise java unit-2_chapter-2
Enterprise  java unit-2_chapter-2Enterprise  java unit-2_chapter-2
Enterprise java unit-2_chapter-2sandeep54552
 
001. Introduction about React
001. Introduction about React001. Introduction about React
001. Introduction about ReactBinh Quan Duc
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Previewvaluebound
 
REST Easy with AngularJS - ng-grid CRUD EXAMPLE
REST Easy with AngularJS - ng-grid CRUD EXAMPLEREST Easy with AngularJS - ng-grid CRUD EXAMPLE
REST Easy with AngularJS - ng-grid CRUD EXAMPLEreneechemel
 

Tendances (20)

React introduction
React introductionReact introduction
React introduction
 
Learning React - I
Learning React - ILearning React - I
Learning React - I
 
Introduce Flux & react in practices (KKBOX)
Introduce Flux & react in practices (KKBOX)Introduce Flux & react in practices (KKBOX)
Introduce Flux & react in practices (KKBOX)
 
3rd Generation Web Application Platforms
3rd Generation Web Application Platforms3rd Generation Web Application Platforms
3rd Generation Web Application Platforms
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Introduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace ITIntroduction to React JS for beginners | Namespace IT
Introduction to React JS for beginners | Namespace IT
 
Breaking the Server-Client Divide with Node.js and React
Breaking the Server-Client Divide with Node.js and ReactBreaking the Server-Client Divide with Node.js and React
Breaking the Server-Client Divide with Node.js and React
 
Introduction to React JS for beginners
Introduction to React JS for beginners Introduction to React JS for beginners
Introduction to React JS for beginners
 
Simple REST with Dropwizard
Simple REST with DropwizardSimple REST with Dropwizard
Simple REST with Dropwizard
 
Its time to React.js
Its time to React.jsIts time to React.js
Its time to React.js
 
Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...
Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...
Think Async: Understanding the Complexity of Multithreading - Avi Kabizon & A...
 
Akka - Developing SEDA Based Applications
Akka - Developing SEDA Based ApplicationsAkka - Developing SEDA Based Applications
Akka - Developing SEDA Based Applications
 
React & Flux Workshop
React & Flux WorkshopReact & Flux Workshop
React & Flux Workshop
 
React.js
React.jsReact.js
React.js
 
The Past Year in Spring for Apache Geode
The Past Year in Spring for Apache GeodeThe Past Year in Spring for Apache Geode
The Past Year in Spring for Apache Geode
 
SpringMVC
SpringMVCSpringMVC
SpringMVC
 
Enterprise java unit-2_chapter-2
Enterprise  java unit-2_chapter-2Enterprise  java unit-2_chapter-2
Enterprise java unit-2_chapter-2
 
001. Introduction about React
001. Introduction about React001. Introduction about React
001. Introduction about React
 
React JS: A Secret Preview
React JS: A Secret PreviewReact JS: A Secret Preview
React JS: A Secret Preview
 
REST Easy with AngularJS - ng-grid CRUD EXAMPLE
REST Easy with AngularJS - ng-grid CRUD EXAMPLEREST Easy with AngularJS - ng-grid CRUD EXAMPLE
REST Easy with AngularJS - ng-grid CRUD EXAMPLE
 

Similaire à Android rest client applications-services approach @Droidcon Bucharest 2012

How to Contribute to Apache Usergrid
How to Contribute to Apache UsergridHow to Contribute to Apache Usergrid
How to Contribute to Apache UsergridDavid M. Johnson
 
Microservices and modularity with java
Microservices and modularity with javaMicroservices and modularity with java
Microservices and modularity with javaDPC Consulting Ltd
 
Architecting single-page front-end apps
Architecting single-page front-end appsArchitecting single-page front-end apps
Architecting single-page front-end appsZohar Arad
 
Breaking Down the Monolith - Peter Marton, RisingStack
Breaking Down the Monolith - Peter Marton, RisingStackBreaking Down the Monolith - Peter Marton, RisingStack
Breaking Down the Monolith - Peter Marton, RisingStackNodejsFoundation
 
Surrogate dependencies (in node js) v1.0
Surrogate dependencies  (in node js)  v1.0Surrogate dependencies  (in node js)  v1.0
Surrogate dependencies (in node js) v1.0Dinis Cruz
 
Sherlock Homepage - A detective story about running large web services - WebN...
Sherlock Homepage - A detective story about running large web services - WebN...Sherlock Homepage - A detective story about running large web services - WebN...
Sherlock Homepage - A detective story about running large web services - WebN...Maarten Balliauw
 
Using Modern Browser APIs to Improve the Performance of Your Web Applications
Using Modern Browser APIs to Improve the Performance of Your Web ApplicationsUsing Modern Browser APIs to Improve the Performance of Your Web Applications
Using Modern Browser APIs to Improve the Performance of Your Web ApplicationsNicholas Jansma
 
540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdf540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdfhamzadamani7
 
ITT 2014 - Erik Hellmann - Android Programming - Smarter and Better Networking
ITT 2014 - Erik Hellmann - Android Programming - Smarter and Better NetworkingITT 2014 - Erik Hellmann - Android Programming - Smarter and Better Networking
ITT 2014 - Erik Hellmann - Android Programming - Smarter and Better NetworkingIstanbul Tech Talks
 
High Performance NodeJS
High Performance NodeJSHigh Performance NodeJS
High Performance NodeJSDicoding
 
JSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendJSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendVlad Fedosov
 
How and why we evolved a legacy Java web application to Scala... and we are s...
How and why we evolved a legacy Java web application to Scala... and we are s...How and why we evolved a legacy Java web application to Scala... and we are s...
How and why we evolved a legacy Java web application to Scala... and we are s...Katia Aresti
 
Ajax tutorial by bally chohan
Ajax tutorial by bally chohanAjax tutorial by bally chohan
Ajax tutorial by bally chohanWebVineet
 
Website Monitoring with Distributed Messages/Tasks Processing (AMQP & RabbitM...
Website Monitoring with Distributed Messages/Tasks Processing (AMQP & RabbitM...Website Monitoring with Distributed Messages/Tasks Processing (AMQP & RabbitM...
Website Monitoring with Distributed Messages/Tasks Processing (AMQP & RabbitM...Jimmy DeadcOde
 

Similaire à Android rest client applications-services approach @Droidcon Bucharest 2012 (20)

Netflix conductor
Netflix conductorNetflix conductor
Netflix conductor
 
Advanced android app development
Advanced android app developmentAdvanced android app development
Advanced android app development
 
Android101
Android101Android101
Android101
 
How to Contribute to Apache Usergrid
How to Contribute to Apache UsergridHow to Contribute to Apache Usergrid
How to Contribute to Apache Usergrid
 
Microservices and modularity with java
Microservices and modularity with javaMicroservices and modularity with java
Microservices and modularity with java
 
Architecting single-page front-end apps
Architecting single-page front-end appsArchitecting single-page front-end apps
Architecting single-page front-end apps
 
Breaking Down the Monolith - Peter Marton, RisingStack
Breaking Down the Monolith - Peter Marton, RisingStackBreaking Down the Monolith - Peter Marton, RisingStack
Breaking Down the Monolith - Peter Marton, RisingStack
 
Surrogate dependencies (in node js) v1.0
Surrogate dependencies  (in node js)  v1.0Surrogate dependencies  (in node js)  v1.0
Surrogate dependencies (in node js) v1.0
 
Sherlock Homepage - A detective story about running large web services - WebN...
Sherlock Homepage - A detective story about running large web services - WebN...Sherlock Homepage - A detective story about running large web services - WebN...
Sherlock Homepage - A detective story about running large web services - WebN...
 
Using Modern Browser APIs to Improve the Performance of Your Web Applications
Using Modern Browser APIs to Improve the Performance of Your Web ApplicationsUsing Modern Browser APIs to Improve the Performance of Your Web Applications
Using Modern Browser APIs to Improve the Performance of Your Web Applications
 
Rest with Spring
Rest with SpringRest with Spring
Rest with Spring
 
540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdf540slidesofnodejsbackendhopeitworkforu.pdf
540slidesofnodejsbackendhopeitworkforu.pdf
 
ITT 2014 - Erik Hellmann - Android Programming - Smarter and Better Networking
ITT 2014 - Erik Hellmann - Android Programming - Smarter and Better NetworkingITT 2014 - Erik Hellmann - Android Programming - Smarter and Better Networking
ITT 2014 - Erik Hellmann - Android Programming - Smarter and Better Networking
 
Cloud APIs Overview Tucker
Cloud APIs Overview   TuckerCloud APIs Overview   Tucker
Cloud APIs Overview Tucker
 
High Performance NodeJS
High Performance NodeJSHigh Performance NodeJS
High Performance NodeJS
 
JSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontendJSFest 2019: Technology agnostic microservices at SPA frontend
JSFest 2019: Technology agnostic microservices at SPA frontend
 
Windows 8 Metro apps and the outside world
Windows 8 Metro apps and the outside worldWindows 8 Metro apps and the outside world
Windows 8 Metro apps and the outside world
 
How and why we evolved a legacy Java web application to Scala... and we are s...
How and why we evolved a legacy Java web application to Scala... and we are s...How and why we evolved a legacy Java web application to Scala... and we are s...
How and why we evolved a legacy Java web application to Scala... and we are s...
 
Ajax tutorial by bally chohan
Ajax tutorial by bally chohanAjax tutorial by bally chohan
Ajax tutorial by bally chohan
 
Website Monitoring with Distributed Messages/Tasks Processing (AMQP & RabbitM...
Website Monitoring with Distributed Messages/Tasks Processing (AMQP & RabbitM...Website Monitoring with Distributed Messages/Tasks Processing (AMQP & RabbitM...
Website Monitoring with Distributed Messages/Tasks Processing (AMQP & RabbitM...
 

Android rest client applications-services approach @Droidcon Bucharest 2012

  • 1. Android REST-client applications: services approach Sharing experience
  • 3. Case New startup
  • 4. Case New startup
  • 5. Case New startup
  • 6. Case New startup
  • 7. Case New startup stores posts sends provides notifies suggests
  • 8. Case New startup REST
  • 9. REST https://api.twitter.com/1/users/show.json?screen_name=roman_mazur { id : 14701612, name : “Roman Mazur”, location : “Ukraine, Kyiv”, … }
  • 10. How to perform requests @Override public void onClick(View view) { URLConnection connection = new URL(…).openConnection(); … }
  • 11. How to perform requests @Override public void onClick(View view) { URLConnection connection = new URL(…).openConnection(); … }
  • 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. 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. Services Way • See also – Google IO 2010 session “Developing Android REST Client Applications”
  • 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. 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. 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. How we perform requests now @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); getLoaderManager().initLoader(1, null, this); }
  • 19. How we perform requests now @Override public 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. How we perform requests now @Override public 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. How we perform requests now @Override public 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. ResponseData • Result code • Status message • User visible message • Data
  • 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. 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. Service Side: Response Pipeline URLConnection ContentHandler Parsed object ResponseDataConverter ResponseData ContentAnalyzer Listeners Analyzed ResponseData
  • 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. It’s open source But we lack documentation :) http://code.google.com/p/enroscar/ and we are preparing to release it on GitHub
  • 28. Thanks! Roman Mazur Head of Android Unit at Stanfy Kyiv GDD co-organizer mazur.roman@gmail.com +Roman Mazur @roman_mazur