Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了有没有人与Android整合ODOO?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在为一个坚持使用Odoo for API的客户开发一个 Android应用程序.我对它没有任何一个想法我甚至在提到这个 link之后都没有得到它.它们提供了一个URL,数据库名称,用户名和密码.如果以前有人用Android做过Odoo,你能提出任何建议吗?

解决方法

有很多方法可以将Android连接到Odoo.他们来了:

> Json-RPC
> XML-RPC(特别是aXMLRPC,这就是我正在使用的)
>还有一个名为Odoo Mobile Framework的框架.我已经尝试过,但发现了很多问题而且我无法正常工作.您可以找到文档here.

Odoo有一个Web Service API,可用于Python,Ruby,PHP和Java.我强烈建议你去看看.

对于我的情况,我克隆了aXMLRPC git存储库,在我的项目中创建了一个包并调整了原始包名.但是最近我在Stack Overflow上发现了this,解释了如何使用Gradle将aXMLRPC添加你的Android项目中(我还没试过).

Odoo提供了三个端点:

> xmlrpc / 2 / db获取服务器上可用数据库的列表,不需要进行身份验证;
> xmlrpc / 2 / common登录服务器,不需要进行身份验证;
> xmlrpc / 2 / object,用于通过execute_kw RPC函数调用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;
    }

}

在这个类中,构造函数作为参数的服务器地址(可以是http(s):// your_ip_address:the_port_number)和路径(‘db’,’common’或’object’).

checkServer方法返回一个对象,该对象实际上是一个包含可用数据库列表的数组.

@L_775_23@mehtod返回一个Integer,它是经过身份验证的用户的Id.

对于Odoo CRUD mehtods(search_read,search_count,search,write,create,unlink),您可以查看与您想要的方法匹配的Odoo Web Service API Java代码.

以下是search_read方法的示例.我假设你有一个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",user_id,password,object,"search_read",conditions,fields);
    } catch (XMLRPCException E) {
        e.printStackTrace();
    }
    return result;
}

哪里

> object是一个Odoo模型,例如“res.partner”
> conditions是域(filter),类似于:Collections.singletonList(Collections.singletonList(Arrays.asList(“supplier”,“=”,truE)));
>字段,您想要获得的字段,

fields = new 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,"is_company");
        String street = OdooutIl.getString((Map<String,"street");  
        int id= OdooutIl.getInteger((Map<String,"id");
    }
}

这里是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,String fieldName) {
        Integer res = 0;
        if (map.get(fieldName) instanceof Integer) {
            res = (Integer) map.get(fieldName);
        }
        return res;
    }

    public static Double getDouble(Map<String,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,String fieldName) {
        Boolean res = false;
        if (map.get(fieldName) instanceof Boolean) {
            res = (Boolean) map.get(fieldName);
        }
        return res;
    }


    public static Float getFloat(Map<String,String fieldName) {
        Float res = 0f;
        if (map.get(fieldName) instanceof Float) {
            res = (Float) map.get(fieldName);
        }
        return res;
    }
}

如果您有many2one字段,则只能访问相关记录的ID和名称.您可以使用以下类来@L_841_21@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;
    }
}
@H_971_9@many2One类的用法示例

String partner_name= Many2One.getMany2One((Map<String,"partner_id").getName();
    int partner_id= Many2One.getMany2One((Map<String,"partner_id").getId();

对于其他剩余的CRUD方法,您可以通过阅读Odoo Web Service API documentation轻松找到它们的工作方式.

我希望这会给你一些见解.

大佬总结

以上是大佬教程为你收集整理的有没有人与Android整合ODOO?全部内容,希望文章能够帮你解决有没有人与Android整合ODOO?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。