我如何使用Java解析xmlrpc respnse(读取Odoo API)

问题描述 投票:0回答:1

Odoo的新手,我必须使用Odoo API的读取方法来获取与产品相关的类别。我得到一个对象列表,然后对于每个类别,我都需要提取字段product_tmpl_ids和一个整数列表。我不确定该怎么做。这是方法:

public List readModelfields(String modelName, List<Integer>ids, List<String> fields, Integer uid)throws XmlRpcException {
    List record = (List)Arrays.asList((Object[])models.execute("execute_kw", Arrays.asList(
            db, uid, password,
            modelName, "read",
             ids,
            new HashMap() {{
                put("fields", fields);
            }}
        )));
    return record;
}

这是其余代码:

List<String> fields = new ArrayList<>();
fields.add("product_tmpl_ids");
List categoryIds = (List<Integer>)service.searchByStrParameter("product.public.category", "name", "A - Administration / Office", uid);
List result = (List)service.readModelfields("product.public.category", categoryIds, fields, uid);

有人可以帮忙吗?如何从获取的对象中提取字段?

api parsing response odoo xml-rpc
1个回答
0
投票

您已将搜索方法(列表)的结果传递给readModelfields,该属性按原样调用read方法,如外部API read方法ids中所述,必须在列表中传递:

Arrays.asList(ids),

您使用搜索,然后在同一型号上进行读取,而是使用search_read以下示例显示如何获取'res.partners'的many2many字段category_id

public List searchReadModelfields(String modelName, List domain, List<String> fields, Integer uid) throws XmlRpcException {
    return (List)Arrays.asList((Object[])models.execute("execute_kw", Arrays.asList(
            db, uid, password,
            modelName,
            "search_read",
            Arrays.asList(domain),
            new HashMap() {{
                put("fields", fields);
            }}
    )));
}

调用searchReadModelfields并打印出值:

List<String> fields = new ArrayList<>();
fields.add("category_id");
List domain = Arrays.asList(
        Arrays.asList("category_id", "!=", false)
);

// return list of hashmaps)
List result = (List)service.searchReadModelfields("res.partner", domain, fields, uid);

HashMap values;
Integer partner_id;
Object[] category_ids;
for(Object partner_data: result) {
    values  = (HashMap)partner_data;
    partner_id = (Integer) values.get("id");
    category_ids = (Object [])values.get("category_id");

    System.out.printf("{partner_id: %s, ", partner_id);
    System.out.print("category_ids:[");
    for(Object category_id: category_ids) {
        System.out.printf("%s,",
                category_id
        );
    }
    System.out.println("]}");
 }
© www.soinside.com 2019 - 2024. All rights reserved.