Skip to content
This repository has been archived by the owner on Jan 14, 2018. It is now read-only.

Starter guide

stephanenicolas edited this page Jan 10, 2013 · 28 revisions

You wanna see what RoboSpice looks like in action ? Great ! This is the page you are looking for.

RoboSpice usage

Setup your project

RoboSpice uses a modular architecture. It offers 3 main modules :

  • robospice-cache that provides caching ;
  • robospice the core library ;
  • robospice-spring-android to use Spring Android to perform REST requests.

RoboSpice offers 3 jars for those modules, they are very easy to include in your projects.

Moreover, robospice-cache module uses optional dependencies to various librairies depending on the cache formats you use. Those formats can also be used by Spring Android, but they can be different. For instance, you can get POJOs from a Web service using Json and save them to cache as Json...or ORMLite. In this latter case, you will annotate twice your POJO classes, once for Json and once for ORMLite. The two tweeter examples of the RoboSpice Motivations app will illustrate the two scenarios.

Under Maven

Basically, you will want to add the RoboSpice library from maven central using this in your pom.xml file :

		<dependency>
			<groupId>com.octo.android.robospice</groupId>
			<artifactId>robospice</artifactId>
			<version>${robospice.version}</version>
		</dependency>

If you want to perform REST requests (using spring android), then you will add this instead :

		<dependency>
			<groupId>com.octo.android.robospice</groupId>
			<artifactId>robospice-spring-android</artifactId>
			<version>${robospice.version}</version>
		</dependency>

Now, you decide what kind of caching format you want to use :

Use one of these dependencies or a combination of them (versions of dependencies are provided in the pom file of robospice-parent project :

for Json support via Jackson :

		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
		</dependency>	

for Json support via Gson :

		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
		</dependency>

for XML support via XML Simple Serializer :

		<dependency>
			<groupId>org.simpleframework</groupId>
			<artifactId>simple-xml</artifactId>
			<exclusions>
		    	<exclusion>
		      		<groupId>xpp3</groupId>
		      		<artifactId>xpp3</artifactId>
	    		</exclusion>
		    	<exclusion>
		      		<groupId>stax</groupId>
		      		<artifactId>stax-api</artifactId>
	    		</exclusion>
  			</exclusions>			
		</dependency>	

for ORMLite support :

        <dependency>
            <groupId>com.j256.ormlite</groupId>
            <artifactId>ormlite-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.j256.ormlite</groupId>
            <artifactId>ormlite-android</artifactId>
        </dependency>	

Under Eclipse

Add the RoboSpice jars from the download section to your libs folder. Also add the jars of the librairies mentioned above according to the cache formats you want to use. You will also need to add the jars of Guava (below 1.3.0) or Apache Commons IO (after 1.3.0, included).

Use one of these dependencies or a combination of them (versions of dependencies are provided in the pom file of robospice-parent project.

Under IntelliJ

TODO

Using RoboSpice

To use RoboSpice in your application, there are 4 steps :

  • 1 has to be performed once for all requests (creating a RoboSpice service) ;
  • 1 has to be done for every Activity class (or only once for your project if you use a common base class for all your activities) ;
  • and 2 steps have to be repeated for each requests (creating a request and a listener).

The snippets below have been extracted from the Json Tweeter example of Robospice Motivations app.

Creating a RoboSpice Service

This part is the most difficult, but you only have to do it once to enable all requests to be processed. You will define a subclass of RoboSpice Service and declare it in the AndroidManifest.xml file.

public class TweeterJsonSpiceService extends SpringAndroidContentService {

    @Override
    public CacheManager createCacheManager( Application application ) {
        CacheManager cacheManager = new CacheManager();

        InFileStringObjectPersister inFileStringObjectPersister = new InFileStringObjectPersister( application );
        InFileInputStreamObjectPersister inFileInputStreamObjectPersister = new InFileInputStreamObjectPersister( application );
        JacksonObjectPersisterFactory inJSonFileObjectPersisterFactory = new JacksonObjectPersisterFactory( application );

        inFileStringObjectPersister.setAsyncSaveEnabled( true );
        inFileInputStreamObjectPersister.setAsyncSaveEnabled( true );
        inJSonFileObjectPersisterFactory.setAsyncSaveEnabled( true );

        cacheManager.addPersister( inFileStringObjectPersister );
        cacheManager.addPersister( inFileInputStreamObjectPersister );
        cacheManager.addPersister( inJSonFileObjectPersisterFactory );
        return cacheManager;
    }

    @Override
    public RestTemplate createRestTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        //find more complete examples in RoboSpice Motivation app
        //to enable Gzip compression and setting request timeouts.

        // web services support json responses
        MappingJacksonHttpMessageConverter jsonConverter = new MappingJacksonHttpMessageConverter();
        FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
        final List< HttpMessageConverter< ? >> listHttpMessageConverters = restTemplate.getMessageConverters();

        listHttpMessageConverters.add( jsonConverter );
        listHttpMessageConverters.add( formHttpMessageConverter );
        listHttpMessageConverters.add( stringHttpMessageConverter );
        restTemplate.setMessageConverters( listHttpMessageConverters );
        return restTemplate;
    }

Then add this to your AndroidManifest.xml file :

        <service
            android:name=".robospice.tweeter.json.TweeterJsonSpiceService"
            android:exported="false" />

Spice your activity

In your activity, or a base class if you use one (preferred), add the following :

    private static final String JSON_CACHE_KEY = "tweets_json";
    private SpiceManager spiceManager = new SpiceManager( TweeterJsonSpiceService.class );

    @Override
    protected void onStart() {
        super.onStart();
        spiceManager.start( this );
    }

    @Override
    protected void onStop() {
        spiceManager.shouldStop();
        super.onStop();
    }

    public void refreshTweets() {
        spiceManager.execute( new TweetJsonRequest(), JSON_CACHE_KEY, DurationInMillis.NEVER, new TweetRequestListener() );
    }

Create a request and a listener

You will repeat this step for each different kind of request. Those steps are easy and intuitive :

public class TweetJsonRequest extends RestContentRequest< ListTweets > {

    public TweetJsonRequest() {
        super( ListTweets.class );
    }

    @Override
    public ListTweets loadDataFromNetwork() throws Exception {
        return getRestTemplate().getForObject( "http://search.twitter.com/search.json?q=android&rpp=20", ListTweets.class );
    }
}

As an inner class of your activity, add a request listener that will update your UI. Don't worry about memory leaks, RoboSpice manages your activity life cycle.

private class TweetRequestListener implements RequestListener< ListTweets > {

        @Override
        public void onRequestFailure( SpiceException arg0 ) {
          //update your UI
        }

        @Override
        public void onRequestSuccess( ListTweets listTweets ) {
          //update your UI
        }
    }

###POJO classes

In this example, we used the following POJOs to receive a list of Tweets using Json via Jackson :

@JsonIgnoreProperties(ignoreUnknown = true)
public class ListTweets {

    private List< Tweet > results;

    public List< Tweet > getResults() {
        return results;
    }

    public void setResults( List< Tweet > results ) {
        this.results = results;
    }
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Tweet {
    private String text;

    public String getText() {
        return text;
    }

    public void setText( String text ) {
        this.text = text;
    }
}

You're done ! Launch your activity, plug the refreshTweets() method to a Button for instance, and enjoy !