android - 将 Retrofit 和 GreenDao 与嵌套的 json 对象一起使用

标签 android json retrofit greendao

我想结合使用 Retrofit 和 GreenDao,但嵌套的 Json 对象有问题。我的嵌套字段仍然是空的。

这是 Json 数据结构

[
    {
        "id": 1, 
        "street": "Streetname", 
        "zipcode": 12345, 
        "city": "MyCity", 
        "phone_number": "+123456789", 
        "position": "12.0000, 9.0000", 
        "company": {
            "title": "CompanyName", 
            "group": {
                "title": "GroupName"
            }
        }
    }
]

我的 DaoGenerator 是这样的

    Entity customItem = schema.addEntity("CustomItems");
    customItem.addIdProperty();
    customItem.addStringProperty("street");
    customItem.addIntProperty("zipcode");
    customItem.addStringProperty("city");
    customItem.addStringProperty("phone_number");
    customItem.addStringProperty("position");

    Entity company = schema.addEntity("Company");
    company.addIdProperty();
    company.addStringProperty("title");

    Entity group = schema.addEntity("Group");
    group.addIdProperty();
    group.addStringProperty("title");

    Property companyPropId = customItem.addLongProperty("companyId").notNull().getProperty();
    customItem.addToOne(company, companyPropId);

    Property groupPropId = company.addLongProperty("groupId").notNull().getProperty();
    company.addToOne(group, groupPropId);

我的问题是 customItem.getCompany() 返回 null 但值“id”到“position”没问题。我不确定问题出在哪里,因为我的 CustomItem 类包含成员

private Company company;

还有公司的二传手,我看不出任何错字。

public void setCompany(Company company) {
    if (company == null) {
        throw new DaoException("To-one property 'companyId' has not-null constraint; cannot set to-one to null");
    }
    synchronized (this) {
        this.company = company;
        companyId = company.getId();
        company__resolvedKey = companyId;
    }
}

最佳答案

我让它运行了,但我遇到了多个问题。

1) 当我想保留 CustomItem、Company 和 Group 时,我遇到了 getter getCompany() 和 getGroup() 返回 null 的问题,因为它们不直接返回成员而是从数据库中获取它。因此,我向生成的 CustomItem 实体类添加了一个 getter,它只返回公司成员。现在我可以将公司插入数据库。 getter 看起来像这样:

// KEEP METHODS - put your custom methods here
public Company getCompanyLocal() {
    return company;
}
// KEEP METHODS END

同样适用于 Company 和 Group。但是还有另一个问题......

2) 第二个问题是实体“Group”,因为“group”是保留的 SQL 关键字。我看到了这个问题的一个解决方案和一个糟糕的解决方法:

  • 好的方法是将您的 json 数据从“group”更改为“business_group”,并据此更改您的 DAO。完成。

  • 如果您遇到像我一样无法更改 json 的相同情况,则糟糕的解决方法可以执行以下操作。我根本不保留该组​​,但可以通过公司访问它。它以某种方式出现在那里。因此,我向我的 Company 类添加了一个 getter,就像上面 CustomItem 的 getter 一样。它有效,但你应该避免这种情况。因为您无法在数据库中查询组或从数据库加载组。

关于android - 将 Retrofit 和 GreenDao 与嵌套的 json 对象一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31120433/

相关文章:

android - 从文件中读取 JSON 到 GSON

android - 如何管理android微调器项目的高度?

java - 在 Android 中使用 OkHttp 客户端时 header 值中出现意外的字符 0x0a

java - 使用 Retrofit 和 GSON 解析 JSON,尝试解析和获取回调时出错。

java - 将选定的微调器项目值设置为编辑 TextView - Android

android - Recyclerview 的最后一项为 "add"项

c# - 使用c#导入json数据

javascript - 数据表-未捕获的类型错误 : Cannot read property 'length' of undefined

PHP - 显示 Json 编码消息

android - 如何使用相同的键但不同的索引发送数组或字符串列表进行改造?