无法使用robospice和ormlite存储JSON请求的嵌套部分

时间:2013-12-02 23:04:52

标签: android json ormlite resttemplate robospice

您好我们使用RoboSpice与ORMLite和SpringAndroidSpiceRequest来阅读 来自网页的JSON信息并写入SQLite数据库。

我们正在使用最新的JSON映射器MappingJackson2HttpMessageConverter。

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

我们已经尝试过读取嵌套的JSON查询,如下例所示,我们可以阅读 并存储到数据库,第一级JSON(页面,总计),但我们无法存储 JSON数组的下一个levet,所有“schedule_date”条目。

我们已经将这个示例(以及更多)作为存储JSON的最佳示例 数据。但它对我们不起作用。 RoboSpice persist JSON array with OrmLite

问题:我们如何读取和编写嵌套的JSON密钥和值,我们 很难过! :o

JSON:

{
   "page": 2,
   "total": 5,
   "result":
   [
       {
           "schedule_date": "2013-08-03"
       },
       {
           "schedule_date": "2013-09-03"
       },
       {
           "schedule_date": "2013-10-03"
       },
       {
           "schedule_date": "2013-11-03"
       },
       {
           "schedule_date": "2013-12-03"
       }
   ]
}

BroadcastResult.java:

            import java.util.Collection;
            import java.util.List;
            import com.j256.ormlite.field.DatabaseField;
            import com.j256.ormlite.table.DatabaseTable;
            import com.j256.ormlite.field.ForeignCollectionField;
            import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

            @JsonIgnoreProperties(ignoreUnknown = true)
            @DatabaseTable(tableName="BroadcastResult")
            public class BroadcastResult {

                @DatabaseField(id = true)
                private int page;

                @DatabaseField
                private int total;

                @ForeignCollectionField(eager = false)
                private Collection<Broadcast> result;

                public int getPage() {
                    return page;
                }

                public void setPage(int page) {
                    this.page = page;
                }

                public int getTotal() {
                    return total;
                }

                public void setTotal(int total) {
                    this.total = total;
                }

                public Collection<Broadcast> getResult() {
                    return result;
                }

                public void setResult(Collection<Broadcast> result) {
                    this.result = result;
                }
            }

Broadcast.java:

            import com.j256.ormlite.field.DatabaseField;
            import com.j256.ormlite.table.DatabaseTable;
            import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

            @JsonIgnoreProperties(ignoreUnknown = true)
            @DatabaseTable(tableName="Broadcast")
            public class Broadcast {

                @DatabaseField(id = true)
                private String schedule_date;

                @DatabaseField(foreign = true)
                private BroadcastResult result;

                public String getScheduleDate() {
                    return schedule_date;
                }

                public void setScheduleDate(String schedule_date) {
                    this.schedule_date = schedule_date;
                }

                public void setBroadcastResult(BroadcastResult result) {
                    this.result = result;
                }

                public BroadcastResult getBroadcastResult() {
                    return result;
                }   
            }

我们得到了这个错误,我们不明白。
Logcat:SQLiteLog(10637):( 1)没有这样的列:result_id

@DatabaseField(foreign = true) // also tried (foreign = true, ColumnName="result")
private BroadcastResult result;

运行测试的完整日志可以在这里看到: https://gist.github.com/anonymous/7760344

1 个答案:

答案 0 :(得分:0)

您是否尝试过添加

@DatabaseField(generatedId = true)
private int _id;

到您的Broadcast.java类?