有没有人将ODOO与Android集成在一起? [英] Is there anyone integratted ODOO with Android?

查看:80
本文介绍了有没有人将ODOO与Android集成在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为坚持使用Odoo for API的客户端开发一个android应用程序.对此我没有任何想法,即使引用此

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:

  1. Json-RPC
  2. XML-RPC(尤其是 aXMLRPC ,这就是我正在使用的语言)
  3. 还有一个名为 Odoo移动框架的框架.我已经尝试过,但发现了很多问题,但无法使其正常运行.您可以在此处找到文档.
  1. Json-RPC
  2. XML-RPC (especially aXMLRPC, this is what I am using)
  3. There is also a framework called Odoo Mobile Framework . I have tried it but found a lot of issues and I was not able to get it work properly. You can find the documentation here.

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:

  1. xmlrpc/2/db获取服务器上可用数据库的列表,不需要进行身份验证;
  2. xmlrpc/2/common登录到服务器,不需要进行身份验证;
  3. xmlrpc/2/object,用于通过execute_kw RPC函数调用odoo模型的方法.

  1. xmlrpc/2/db to get the list of available databases on your server, it does not require to be authenticated;
  2. xmlrpc/2/common to log in to the server, it does not require to be authenticated;
  3. 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

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;
}

哪里

  1. 对象是一个Odoo模型,例如"res.partner"
  2. 条件是域(过滤器),如下所示:Collections.singletonList(Collections.singletonList(Arrays.asList("supplier", "=", true)));
  3. 字段,您想要获取的字段,

  1. object is an Odoo model for example "res.partner"
  2. conditions is the domain (filter) something like this: Collections.singletonList(Collections.singletonList(Arrays.asList("supplier", "=", true)));
  3. 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集成在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆