我目前正在为坚持使用Odoo for API的客户端开发一个android应用程序.对此我没有任何想法,即使引用此链接.它们提供了URL,数据库名称,用户名和密码.如果以前有人使用Android进行过Odoo,你能提出任何建议吗?
I am currently developing an android application for a client who is insisting to use Odoo for API.I don't have any single idea about it I am not getting it any even after referring this link.They provide a URL, Database name, username, and password.If any one did Odoo with Android before, Can you give any suggestions?
推荐答案有很多方法可以将Android连接到Odoo.他们在这里:
There are a lot of ways to connect Android to Odoo. Here they are:
Odoo具有 Web服务API ,可用于Python,Ruby, PHP和Java.我强烈建议您看一看.
Odoo has a Web Service API which is available for Python, Ruby, PHP and Java. I strongly recommend to take a look.
对于我来说,我已经克隆了aXMLRPC git存储库,在我的项目中创建了一个程序包,并修改了原始的程序包名称.但是最近我发现了此 Stack Overflow上的内容介绍了如何使用Gradle将XMLRPC添加到您的Android项目(我还没有尝试过).
For my case, I have cloned the aXMLRPC git repository, created a package in my project and adapted the original package name. But recently I have found this on Stack Overflow explaining how to add aXMLRPC to your Android project using Gradle (I didn't give it a try yet).
Odoo提供了三个端点:
Odoo had made available three endpoints:
xmlrpc/2/object,用于通过execute_kw RPC函数调用odoo模型的方法.
xmlrpc/2/object, is used to call methods of odoo models via the execute_kw RPC function. 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; } }
在此类中,构造函数将服务器地址(可以是http(s)://your_ip_address:the_port_number)和path ('db', 'common' or 'object')作为参数.
In this class, the constructor as arguments the server address (it can be http(s)://your_ip_address:the_port_number) and the path ('db', 'common' or 'object').
checkServer方法返回一个对象,该对象实际上是一个包含可用数据库列表的数组.
The checkServer method returns an object which is actually an array containing the list of available databases.
登录方法将返回一个整数,该整数是已验证用户的ID.
The login mehtod returns an Integer which is the Id of the authenticated user.
对于Odoo CRUD方法(搜索读取,搜索计数,搜索,写入,创建,取消链接),您可以查看Odoo Web服务API 匹配所需方法的Java代码.
For the Odoo CRUD mehtods (search_read, search_count, search, write, create, unlink) you can take a look to the Odoo Web Service API Java code matching the method you want.
这是search_read方法的一个示例.我假设您有一个名为client的XMLRPCClient.
Here is an example of the search_read method. I assume that you've an XMLRPCClient named client.
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; }哪里
字段,您想要获取的字段,
fields, the fields you want to get, fields = new HashMap() {{put("fields", Arrays.asList("id","name","is_company","street")); }};
您必须将方法的结果强制转换为Object [],这将为您提供一个数组,其中包含一个对象列表,每个对象代表一个记录.
You must cast the result of the method to Object[] which will give you an array containing a list of objects each representing a record.
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"); } }这里是OdooUtil类
Here the OdooUtil class
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; } }如果您有一个many2one字段,则只能访问相关记录的ID和名称.您可以使用以下类获取many2one记录的ID和名称.
If you have a many2one field you only have access to the id and the name of the related record. You can use the following class to get the id and the name of the many2one record.
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; } }Many2One类的用法示例
Example of usage of Many2One class
String partner_name= Many2One.getMany2One((Map<String, Object>) object, "partner_id").getName(); int partner_id= Many2One.getMany2One((Map<String, Object>) object, "partner_id").getId();对于其他剩余的CRUD方法,您可以通过阅读 Odoo Web服务API文档.
For other remaining CRUD methods, you can easily find a way how they work by reading the Odoo Web Service API documentation.
我希望这能给您一些见识.
I hope this gives you some insights.
更多推荐
有没有人将ODOO与Android集成在一起?
发布评论