Background
I have been trying to modify the Robospice Ormlite example code, which I have been successful to run. The only changes I have made is that I have a JSON response that contains an array of objects.
I created 2 classes:
public class Foos extends ArrayList<Foo>
public class Foo
I had initial problems, as my JSON response is a pure array with no outer container. I read via google groups that the way to solve this is to create a "fake" class (Foos) that extends an ArrayList. This produced the following code:
public class FooRequest extends SpringAndroidSpiceRequest< Foos > {
private String baseUrl;
public FooRequest() {
super( Foos.class );
this.baseUrl = "http://somewhere.com/jsonurl";
}
@Override
public Foos loadDataFromNetwork() throws RestClientException {
Ln.d( "Call web service " + baseUrl );
return getRestTemplate().getForObject( baseUrl, Foos.class );
}
}
I then created the following service code, adapted from the examples.
public class MySpiceService extends SpringAndroidSpiceService {
private static final int WEBSERVICES_TIMEOUT = 10000;
@Override
public CacheManager createCacheManager( Application application ) {
CacheManager cacheManager = new CacheManager();
List< Class< ? >> classCollection = new ArrayList< Class< ? >>();
// add persisted classes to class collection
classCollection.add( Foos.class );
// init
RoboSpiceDatabaseHelper databaseHelper = new RoboSpiceDatabaseHelper( application, "sample_database.db", 2 );
InDatabaseObjectPersisterFactory inDatabaseObjectPersisterFactory = new InDatabaseObjectPersisterFactory( application, databaseHelper, classCollection );
cacheManager.addPersister( inDatabaseObjectPersisterFactory );
return cacheManager;
}
@Override
public RestTemplate createRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
// set timeout for requests
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setReadTimeout( WEBSERVICES_TIMEOUT );
httpRequestFactory.setConnectTimeout( WEBSERVICES_TIMEOUT );
restTemplate.setRequestFactory( httpRequestFactory );
// web services support xml 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;
}
}
The problem
The code runs without any force closes however I never hit the RequestListener inner class within my activity.Neither the success or failure message methods are fired, and it seems quite difficult to debug, so it feels like A "silent failure".
public final class MyRequestListener implements RequestListener< Foos > {
@Override
public void onRequestFailure( SpiceException spiceException ) {
Toast.makeText( SampleSpiceActivity.this, "failure", Toast.LENGTH_SHORT ).show();
}
@Override
public void onRequestSuccess( final Articles result ) {
Toast.makeText( SampleSpiceActivity.this, "success", Toast.LENGTH_SHORT ).show();
}
}
What I have tried
I have tried to annotate the class Foo, with Ormlite annotations, to see if the library needed a helping hand, but still no luck. As outlined below:
@DatabaseTable
public class Foo {
@DatabaseField(generatedId = true)
private int id;
@DatabaseField
private String someText;
}
I wonder if this is a result of the Foos class when I actually want a database table storing Foo. Any advice would be great!