Robospice存储对象在数据库中通过Ormlite延伸的ArrayList数据库中、对象、Robospice、Ormlite

2023-09-06 11:24:10 作者:爱眼爱演碍眼

背景

我一直在试图修改 Robospice Ormlite例子code ,我已经成功运行。我做了唯一的变化是,我有一个包含对象的数组JSON响应。

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.

我在2班:

public class Foos extends ArrayList<Foo>
public class Foo

我最初的问题,因为我的JSON响应是一个纯粹的阵列,没有外部容器。我通过谷歌群体,解决这个问题的方法是创建一个假级(FOOS)扩展一个ArrayList读取。这产生了以下code:

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 );
    }
}

然后,我创建了以下服务code,改编自例子。

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;
    }

}

问题

在code运行但没有任何力量关闭我从来没有打成功或失败消息的方法被解雇我activity.Neither内RequestListener内部类,它似乎相当困难的调试,所以感觉就像一个沉默失败。

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();

        }
    }

我已经试过

我试图来注释类Foo,与Ormlite注释,看是否所需的函数库了援助之手,但仍没有运气。简介如下:

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;

}

我不知道这是FOOS类的结果,当我真正想要的数据库表存储美孚。任何意见将是巨大的!

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!

推荐答案

这个问题是不是真的在RoboSpice一个错误,但ORM精简版模型的限制。

This question is not really a bug in RoboSpice, but a limitation of the ORM Lite model.

如果你想序列化的Foo使用ORMLite集合,则必须使用序列化类,它是美孚的集合。你的做法是正确的。然而,你不能摆脱它单独存放美孚元素。你不仅必须使用FOOS类解析响应也将数据存储在数据库中。

If you want to serialize a collection of Foo using ORMLite, then you must use serialize a class that is a collection of Foo. You approach is right. Nevertheless, you can't just get rid of it to store the Foo elements individually. You not only have to use the Foos class to parse the response but also to store data in the database.

FOOS应该:

定义一个id 使用DataBaseTable和数据字段和外国收集被标注

现在的问题是,你不能注释一个类,并说:我是一个Foreig集。此批注只能用于上字段,而不是类。所以,如果你的类将持有的集合,将工作,但不是一类是一个集合。

The problem is that you can't annotate a class and saying "I am a Foreig Collection". This annotation can only be used on fields, not on classes. So If your class would "hold" a collection, that would work, but not for a class that "is a" collection.

所以,我认为,简单的答案是:不,你不能做到这一点与ORM精简版。则必须存储一个顶级像FOOS,但它应该ORM的精简版是可序列化,为此,它需要有一个集合场,并不能直接成为一个集合

So, I believe that the short answer is : no, you can't do that with ORM Lite. You must store a top class like Foos, but it should be serializable for ORM Lite and for this it needs to have a collection field, and can't be a collection directly.

 
精彩推荐