将 Retrofit 2 与 Greendao 实体相结合

Combining Retrofit 2 with Greendao entities

对于嵌套的 json 个对象,如

{"members": [
    {
        "id":366,
        "code":"T1234",
        "defaultAddress": {
            "addressLine1": "A2 Rityayan co-operative Housing Society"
            "addressLine2": "P-34 Moti jheel avenue Dumdum"
        }
    }
]}

我在 Greendao 中制作了以下实体,

private static void addMember(Schema schema) {
    Entity member = schema.addEntity("CareMember");

    Property memberId = member.addLongProperty("id").primaryKey().getProperty();
    member.addStringProperty("code").unique();

    Entity defaultAddress = schema.addEntity("DefaultAddress");
    defaultAddress.addIdProperty();
    defaultAddress.addStringProperty("addressLine1");
    defaultAddress.addStringProperty("addressLine2");
    member.addToOne(defaultAddress, memberId);
    defaultAddress.implementsSerializable();      
}

我想创建一对一关系并将模型保存在数据库中 虽然保存了成员,但是保存默认地址很难恢复。 我已经设计了一些解决方法,但希望有一个合适的方法来解决这个问题。任何帮助将不胜感激

我找到了一种方法,在改造中使用 jackson 而不是 gson。 为此,在 gradle

中添加以下内容
compile 'com.squareup.retrofit2:converter-jackson:2.0.0'
compile 'org.codehaus.jackson:jackson-core-asl:1.1.0'

然后在休息服务class,

public class RestService{
RestInterface restInterface;
public RestService(Context context) {
        this.context = context;
        OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();


        okHttpClient = clientBuilder.build();

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        restServiceInterface = new Retrofit.Builder() //restServiceInterface //is the the rest service Interface variable
                .baseUrl(RestInterface.BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(JacksonConverterFactory.create(objectMapper))

                .build()
                .create(RestServiceInterface.class);
    }

}

由于jackson使用了setter方法,因此修改CareMember中defaultAddress的setter方法class

 // KEEP METHODS - put your custom methods here
    @JsonSetter(value = "defaultAddress")
    public void setDefaultAddressFromRetrofit(DefaultAddress defaultAddress) {
        if (defaultAddress == null) {
            throw new DaoException("To-one property 'defaultAddressId' has not-null constraint; cannot set to-one to null");
            defaultAddress = new DefaultAddress();
        }

       synchronized (this) {
            this.defaultAddress = defaultAddress;
            defaultAddress.setId(id);
            defaultAddress__resolvedKey = id;
        }

    }
// KEEP METHODS END

最后在您的自定义 Sql 助手中 class,(从您存储或检索的位置)

public class DaoHelper{
public void addCareMember(CareMember entity){
        SQLiteDatabase db = application.getDatabase(true);//application->instance of Application
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession session = daoMaster.newSession();

            daoSession.getDefaultAddressDao().insertOrReplace(entity.getDefaultAddress());


        daoSession.getCareMemberDao().insertOrReplace(entity);
    }

public CareMember getCareMemberByCode(String code) {
        SQLiteDatabase db = application.getDatabase(false);

        CareMember member = (new DaoMaster(db)).newSession().getCareMemberDao()
                .queryRaw("WHERE " + CareMemberDao.Properties.Code.name + "=?", code).get(0);
        member.setDefaultAddress(member.getDefaultAddress());
        member.setContacts(member.getContacts());//this is done here because //the daoSession has to be alive to retrieve from one-many relation
        return member;
    }
}