背景
我一直在尝试修改已成功运行的Robospice Ormlite 示例代码。我所做的唯一更改是我有一个包含对象数组的 JSON 响应。
我创建了 2 个类:
public class Foos extends ArrayList<Foo>
public class Foo
我遇到了最初的问题,因为我的 JSON 响应是一个没有外部容器的纯数组。我通过 google groups 了解到,解决这个问题的方法是创建一个扩展 ArrayList 的“假”类(Foos)。这产生了以下代码:
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 );
}
}
然后,我根据示例创建了以下服务代码。
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;
}
}
问题
代码在没有任何强制关闭的情况下运行,但是我从来没有在我的活动中点击 RequestListener 内部类。成功或失败消息方法都没有被触发,而且似乎很难调试,所以感觉就像一个“无声的失败”。
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();
}
}
我试过的
我尝试使用 Ormlite 注释对 Foo 类进行注释,以查看该库是否需要帮助,但仍然没有运气。如下所述:
@DatabaseTable
public class Foo {
@DatabaseField(generatedId = true)
private int id;
@DatabaseField
private String someText;
}
当我实际上想要一个存储 Foo 的数据库表时,我想知道这是否是 Foos 类的结果。任何建议都会很棒!