Nou*_*fal 3 android xml-rpc openerp odoo-mobile
我目前正在为坚持使用Odoo for API的客户端开发一个android应用程序。我对此没有任何想法,即使在引用此链接后也没有得到它。他们提供了URL,数据库名称,用户名,以及密码。如果以前有人使用Android进行过Odoo的操作,您能提出建议吗?
有很多方法可以将Android连接到Odoo。他们来了:
Odoo具有Web服务API,可用于Python,Ruby,PHP和Java。我强烈建议您看看。
就我而言,我已经克隆了aXMLRPC git存储库,在项目中创建了一个程序包,并修改了原始程序包名称。但是最近我在Stack Overflow上发现了这一点,它解释了如何使用Gradle将XMLRPC添加到您的Android项目中(我还没有尝试过)。
Odoo提供了三个端点:
xmlrpc/2/db 要获取服务器上可用数据库的列表,不需要进行身份验证;xmlrpc/2/common 登录服务器,不需要进行身份验证;xmlrpc/2/object,用于通过execute_kwRPC函数调用odoo模型的方法。
public class OdooConnect {
String url;
private XMLRPCClient client;
public OdooConnect(String serverAddress, String path) {
url = serverAddress + "/xmlrpc/2/" + path;
client = new XMLRPCClient(url);
}
public Object login(String db, String username, String password) {
Object object;
try {
object = client.call("login", db, username, password);
return object;
} catch (XMLRPCException e) {
e.printStackTrace();
}
return null;
}
public Object checkServer() {
Object object;
try {
object = client.call("list", new Object[]{});
return object;
} catch (XMLRPCException e) {
e.printStackTrace();
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)在此类中,构造函数将服务器地址(可以是http(s)://your_ip_address:the_port_number)和用作参数path ('db', 'common' or 'object')。
checkServer方法返回一个对象,该对象实际上是一个包含可用数据库列表的数组。
登录方法返回一个整数,该整数是已认证用户的ID。
对于Odoo CRUD方法(search_read,search_count,搜索,写入,创建,取消链接),您可以查看与所需方法匹配的Odoo Web Service API Java代码。
这是search_read方法的示例。我假设您有一个名为client的XMLRPCClient。
public Object search_read(String db, int user_id, String password, String object, List conditions, Map<String, List> fields) {
Object result = null;
try {
result = client.call("execute_kw", db, user_id, password, object, "search_read", conditions, fields);
} catch (XMLRPCException e) {
e.printStackTrace();
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
哪里
"res.partner"Collections.singletonList(Collections.singletonList(Arrays.asList("supplier", "=", true)));字段,您想要获取的字段,
字段=新的HashMap(){{put(“ fields”,Arrays.asList(“ id”,“ name”,“ is_company”,“ street”)); }};您必须将方法的结果强制转换为Object [],这将为您提供一个数组,其中包含一个对象列表,每个对象代表一个记录。
Object[] objects = (Object[]) result;
if (objects.length > 0) {
for (Object object : objects) {
String name= OdooUtil.getString((Map<String, Object>) object, "name");
boolean is_company= OdooUtil.getBoolean((Map<String, Object>) object, "is_company");
String street = OdooUtil.getString((Map<String, Object>) object, "street");
int id= OdooUtil.getInteger((Map<String, Object>) object, "id");
}
}
Run Code Online (Sandbox Code Playgroud)
这里是OdooUtil类
public class OdooUtil {
public static String getString(Map<String, Object> map, String fieldName) {
String res = "";
if (map.get(fieldName) instanceof String) {
res = (String) map.get(fieldName);
}
return res;
}
public static Integer getInteger(Map<String, Object> map, String fieldName) {
Integer res = 0;
if (map.get(fieldName) instanceof Integer) {
res = (Integer) map.get(fieldName);
}
return res;
}
public static Double getDouble(Map<String, Object> map, String fieldName) {
Double res = 0.0;
if (map.get(fieldName) instanceof Double) {
res = (Double) map.get(fieldName);
}
return res;
}
public static Boolean getBoolean(Map<String, Object> map, String fieldName) {
Boolean res = false;
if (map.get(fieldName) instanceof Boolean) {
res = (Boolean) map.get(fieldName);
}
return res;
}
public static Float getFloat(Map<String, Object> map, String fieldName) {
Float res = 0f;
if (map.get(fieldName) instanceof Float) {
res = (Float) map.get(fieldName);
}
return res;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您有一个many2one字段,则只能访问相关记录的ID和名称。您可以使用以下类获取many2one记录的ID和名称。
public class Many2One {
private int id;
private String name;
public Many2One() {
}
public static Many2One getMany2One(Map<String, Object> stringObjectMap, String fieldName) {
Integer fieldId = 0;
String fieldValue = "";
Many2One res = new Many2One();
if (stringObjectMap.get(fieldName) instanceof Object[]) {
Object[] field = (Object[]) stringObjectMap.get(fieldName);
if (field.length > 0) {
fieldId = (Integer) field[0];
fieldValue = (String) field[1];
}
}
res.id = fieldId;
res.name = fieldValue;
return res;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
Many2One类的使用示例
String partner_name= Many2One.getMany2One((Map<String, Object>) object, "partner_id").getName();
int partner_id= Many2One.getMany2One((Map<String, Object>) object, "partner_id").getId();
Run Code Online (Sandbox Code Playgroud)
对于其他剩余的CRUD方法,您可以通过阅读Odoo Web服务API文档轻松找到其工作方式。
我希望这能给您一些见识。
| 归档时间: |
|
| 查看次数: |
2519 次 |
| 最近记录: |