Friday, June 19, 2015

Android Robospice Framework

Robospice Framework
Introduction:
RoboSpice is a modular android library that makes writing asynchronous long running tasks and network requests easily. It is specialized in network requests, supports caching and offers REST requests out-of-the box using extension modules.One of the libraries to correctly implement the service-based API pattern is RoboSpice.
There are 5 different types of extensions in Robospice.
  1. Spring Android module
  2. Google Http Java Client module
  3. Retrofit module
  4. ORMLite module
  5. UI SpiceList module

For Android we are using Spring Android module.

Spring Android module:
Spring Android module is a library that makes it easy to perform REST requests and communicate with web services using POJOs.
The RoboSpice Spring Android module provides support for
  • All supported network data format of Spring Android : JSON (via Gson/Jackson) and XML
  • Persistence of the results of SpiceRequest in these formats
  • Pre-setup SpiceService subclasses
Using RoboSpice is done in 4 easy steps:
  1. Define the Service, you can use pre-existing SpiceService definitions or create your own.
  2. Create a base activity class which adds a SpiceManager instance to communicate to the service. You will now have to inherit from this activity when creating new ones.
  3. Create a SpiceRequest object that deals with running the asynchronous work, i.e. connect to the server and parse the content.
  4. Create a RequestListener private class in your activity that will receive the response from the SpiceRequest. The service will always trigger the activity if it is in the resumed state, whether or not the response has succeeded.
The beauty of this library is that you can combine it with libraries such as RetroFit and Jackson or GSON. For example, how you parse your response content or build up your SpiceRequest depends on which addons you use.
Robospice framework architecture and request flow:


Background processing is implemented using a dedicated Android service that seamlessly binds to a started activity and reliably unlinks from it when it gets destroyed, continuing to work behind the scenes, caching and serving the results as soon as the activity is bound back again.





Advantages of using Robospice:

  • Supports Android starting from SDK version 8 (Froyo / 2.2.x).
  • Executes network requests asynchronously (in a background Android Service).
  • Supports REST out of the box (using Spring Android or Google Http Client or Retrofit).
  • Is strongly typed ! You query web services using POJOs as parameters and you get POJOs as request results.
  • Enforces no constraints neither on POJOs used for requests nor on Activity classes you use in your projects.
  • Caches results in Json with both Jackson or Jackson2 or Gson, or Xml, or flat text files, or binary files, even using ORMLite (still in beta).
  • Notifies your activities (or any other context) of the result of the network request with respect to their lifecycles.
  • Notifies your activities (or any other context) on the UI Thread.
  • No memory leaks at all, like Android Loaders, unlike Android AsyncTasks.
  • Uses a simple but robust exception handling model.
  • Supports multi-threading of request executions.
  • Is stable, efficient and designed to respect Android philosophy.
  • Supports request cancellation, request prioritization and requests aggregation.
  • Supports aggregation of different web services.
  • Is a full featured replacement for long running Async Tasks even if they are not related to networking.
  • It is open source.
  • And tested (more than 200 tests).

No comments:

Post a Comment