RoboSpice使用OrmLite保留JSON数组 [英] RoboSpice persist JSON array with OrmLite

查看:160
本文介绍了RoboSpice使用OrmLite保留JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将RoboSpice与Spring for Android结合使用,并希望使用OrmLite保留对象的JSON数组. GSON用于JSON编组.使用默认缓存,一切都会按预期进行.但是OrmLite似乎不喜欢对象数组.

I'm using RoboSpice with Spring for Android and would like to persist a JSON array of objects with OrmLite. GSON is used for the JSON marshalling. With the default caching everything works as expected. But OrmLite doesn't seem to like the array of objects.

这是JSON的简化版本:

This is a simplified version of the JSON:

[{"id": 1, "title": "Test 1"},{"id": 2, "title": "Test 3"},{"id": 3, "title": "Test 3"}]

我想将此保留在以下对象中:

I would like to persist this in the following object:

@DatabaseTable
public class Foo {
    @DatabaseField(id = true)
    private int id;
    @DatabaseField
    private String title;

    // getters and setters
    ...
}

基于RoboSpice OrmLite示例,我创建了以下GsonSpringAndroidSpiceService类来添加OrmLite CacheManager.问题就从这里开始.

Based on the RoboSpice OrmLite example I've created the following GsonSpringAndroidSpiceService class to add the OrmLite CacheManager. This is where the problem starts.

public class CustomGsonSpringAndroidSpiceService extends GsonSpringAndroidSpiceService
{
    @Override
    public CacheManager createCacheManager(Application application)
    {
        // add persisted classes to class collection
        List<Class<?>> classCollection = new ArrayList<Class<?>>();
        classCollection.add(Foo.class);

        // init
        CacheManager cacheManager = new CacheManager();
        cacheManager.addPersister(new InDatabaseObjectPersisterFactory(
            application, new RoboSpiceDatabaseHelper(
                application, "database.db", 1), classCollection));
        return cacheManager;
    }
}

这将导致以下错误:

RequestProcessor.java:174(22356): java.lang.RuntimeException: Class [Lcom.example.model.Foo; is not handled by any registered factoryList

当我将classCollection.add(Foo.class);更改为classCollection.add(Foo[].class);时 我收到以下错误:

When I change classCollection.add(Foo.class); to classCollection.add(Foo[].class); I get the following error:

RequestProcessor.java:174(22601): 14:42:23.112 pool-5-thread-1 An unexpected error occured when processsing request CachedSpiceRequest [requestCacheKey=foo, cacheDuration=-1, spiceRequest=com.example.app.FooRequest@4055df40]
RequestProcessor.java:174(22601): java.lang.IllegalArgumentException: No fields have a DatabaseField annotation in class [Lcom.example.app.model.Foo;

有人知道如何使用OrmLite CacheManager处理JSON数组吗?

Anyone an idea how to handle a JSON array with the OrmLite CacheManager ?

推荐答案

我找到了解决此问题的方法.我添加了一个额外的结果对象,该对象保存对象数组.当然,这只有在您能够操纵JSON的情况下才有可能.对此我仍然不是很满意,因为我为模型引入了一个无用的类.

I've found a work around to this problem. I added an extra result object which holds the array of objects. Off course this is only possible if you are able to manipulate the JSON. Still not really happy with this because I have introduce an useless class to my model.

所以我的JSON看起来像:

So my the JSON looks like:

{ 
    "id": 1,
    "result":[{"id": 1, "title": "Test 1"},{"id": 2, "title": "Test 3"},{"id": 3, "title": "Test 3"}]
}

我添加了以下类来保存JSON结果:

And I added the following class to hold the JSON result:

@DatabaseTable
public class FooResult {
    @DatabaseField(id = true)
    private int id;
    @ForeignCollectionField(eager = false)
    private Collection<Foo> result;

    // getters and setters
    ...
}

还在Foo类中添加了外部关系:

Also added the foreign relation the the Foo class:

@DatabaseTable
public class Foo {
    @DatabaseField(id = true)
    private int id;
    @DatabaseField
    private String title;
    @DatabaseField(foreign = true)
    private FooResult result;

    // getters and setters
    ...
}

这篇关于RoboSpice使用OrmLite保留JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆