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

Using Retrofit and GreenDao with nested json objects

我想结合 Retrofit 和 GreenDao,但嵌套 Json-Objects 有问题。我的嵌套字段仍然是空的。

这是 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() returns 为空,但值 "id" 到 "position" 没问题。我不确定问题出在哪里,因为我的 CustomItem class 包含成员

private Company company;

和公司的 setter,我看不到任何错字。

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 时,我遇到了 getters getCompany() 和 getGroup() returned null 的问题,因为它们没有return 成员直接从数据库中获取。因此,我向生成的 CustomItem 实体 class 添加了一个 getter,它只是 return 公司成员。现在我可以将公司插入数据库。 getter 看起来像这样:

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

同样适用于公司和集团。但是还有一个问题...

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

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

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

  • 查询您的数据库以获取组或负载组

要解决您的第二个问题,请将此代码添加到您的 DAO 生成器中:

beacon.addStringProperty("business_group"); //Daogenerator

并将此代码添加到您的网络管理器中:

//add this into your network manager
FieldNamingStrategy strategy = new FieldNamingStrategy() {
        @Override
        public String translateName(Field field) {
            if(field.getName().equalsIgnoreCase("business_group")) {
                return "group";
            } else {
                return field.getName();
            }
        }
    };

并将此 属性 设置为您的 Gson:

//add this in your Gson
.setFieldNamingStrategy(strategy)

希望对您有所帮助!!