0

我使用 greenDAO 作为我的 ORM 工具,并且在使用 GSON 解析 JSON 后将数据保存到表中时遇到了一些问题。在将其保存到数据库之前,我得到了完整的列表。在这里我附上代码片段。请看看并给我你的建议。

MainGenerator.java

public class MainGenerator {

    private static final String PROJECT_DIR = System.getProperty("user.dir");

    public static void main(String[] args) {
        Schema schema = new Schema(1, "com.example.cisystem3.mobiledeliveryndispatch.dao");
        schema.enableKeepSectionsByDefault();

        addTables(schema);

        try {
            new DaoGenerator().generateAll(schema, PROJECT_DIR + "\\app\\src\\main\\java");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
private static void addTables(final Schema schema) {
    Entity setting = addSetting(schema);

    ...

    Entity item = addItems(schema);

    ...
}
private static Entity addItems(final Schema schema) {
    Entity item = schema.addEntity("ItemDetails");
    item.addStringProperty("itemNum");
    item.addStringProperty("itemDesc");
    item.addStringProperty("uom");

    return item;
}

GreenDAO 使用 MainGenerator ItemDetails,ItemDetailsDAO 生成了两个自动文件。现在,在从 WcfService 获取数据时,我创建了一个额外的类 ITEMDETAIL.java,它扩展了 ItemDetails 类,如下所示

ItemDetails.java

public class ItemDetails {

    private String itemNum;
    private String itemDesc;
    private String uom;

    // KEEP FIELDS - put your custom fields here
    // KEEP FIELDS END

    public ItemDetails() {
    }

    public ItemDetails(String itemNum, String itemDesc, String uom) {
        this.itemNum = itemNum;
        this.itemDesc = itemDesc;
        this.uom = uom;
    }

    public String getItemNum() {
        return itemNum;
    }

    public void setItemNum(String itemNum) {
        this.itemNum = itemNum;
    }

    public String getItemDesc() {
        return itemDesc;
    }

    public void setItemDesc(String itemDesc) {
        this.itemDesc = itemDesc;
    }

    public String getUom() {
        return uom;
    }

    public void setUom(String uom) {
        this.uom = uom;
    }

}

项目详细信息.java

@Generated("org.jsonschema2pojo")
public class ITEMDETAIL extends ItemDetails {
    @SerializedName("ITEMDESC")
    @Expose
    private String iTEMDESC;
    @SerializedName("ITEMNO")
    @Expose
    private String iTEMNO;
    @SerializedName("LOCATION")
    @Expose
    private String lOCATION;
    @SerializedName("PRICELIST")
    @Expose
    private String pRICELIST;
    @SerializedName("QTYONHAND")
    @Expose
    private Integer qTYONHAND;
    @SerializedName("UNITPRICE")
    @Expose
    private Double uNITPRICE;
    @SerializedName("UOM")
    @Expose
    private String uOM;

    //Getters and Setters
}

如下使用 GSON 解析 JSON 并创建 List

if (response != null) {

    JSONObject jsonObject = response.getJSONObject(Constants.GetProductionResult);

    JSONArray jsonArray = jsonObject.getJSONArray(Constants.GetItemDetails);

    Gson gson = new Gson();
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
        ITEMDETAIL itemdetail = gson.fromJson(jsonObject1.toString(), ITEMDETAIL.class);
        posts.add(itemdetail);
    }
}

创建列表后,我会尝试将其保存在表中,如下所示

@Override
public synchronized void insertItemDetails(ArrayList<ITEMDETAIL> itemList) {
    try {
        if (itemList != null) {
//Values came here
            for (ItemDetails item : itemList) {
                openWritableDb();
                ItemDetailsDao itemDetailsDao = daoSession.getItemDetailsDao();
                itemDetailsDao.insert(item);
                daoSession.clear();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
4

1 回答 1

0

通过更改 for 循环,插入是可能的:

for (ITEMDETAIL item : itemList) {

    ItemDetails details = new ItemDetails();
    details.setItemDesc(item.getITEMDESC());
    details.setItemNum(item.getITEMNO());
    details.setUom(item.getUOM());

    openWritableDb();
    ItemDetailsDao itemDetailsDao = daoSession.getItemDetailsDao();
    itemDetailsDao.insert(details);
    daoSession.clear();

}
于 2016-11-08T19:13:26.173 回答