Odoo中文网|Odoo实施培训

 找回密码
 立即注册
搜索
热搜: Odoo OpenERP 实施
查看: 7720|回复: 0
打印 上一主题 下一主题

网上的OpenERP-XML-RPC的Jar包应用

[复制链接]

119

主题

119

帖子

566

积分

高级会员

Rank: 4

积分
566
跳转到指定楼层
楼主
发表于 2016-10-12 22:35:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
OpenERP的每一个模块里都有相应的增删改查的方法供我们来对数据库的数据进行操作,而有时候我们希望在远程调用某一个模块中的相应方法,比如我们移动终端设备上的调用。这个时候,我们可以运用XML-RPC进行方法的调用,下面对用XML-RPC对OpenERP进行数据操作的方法介绍:

连接操作:

public class Connect{
    public int Connect(String host,int port,String db,String username,String password){

        int flag = 0;
        XmlRpcClient xmlrpclogin = new XmlRpcClient();

        XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();

        xmlrpcConfigLogin.setEnabledForExceptions(true);
        try {
            xmlrpcConfigLogin.setServerURL(new URL("http", host, port,
                    "/xmlrpc/common"));
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        xmlrpclogin.setConfig(xmlrpcConfigLogin);
        try {
            Object[] params = new Object[] { db, username, password };
            Object id = xmlrpclogin.execute("login", params);
            if (id instanceof Integer)
                flag = (Integer) id;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
}
查找操作:

public class Search{
    public Object Search(String host,int port,String db,String username,String password,String carCode,int id){
        XmlRpcClient xmlrpc = new XmlRpcClient();
        XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();
        xmlrpcConfgDb.setEnabledForExceptions(true);
        try {
            xmlrpcConfgDb.setServerURL(new URL("http", host, port,
                    "/xmlrpc/object"));
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        xmlrpc.setConfig(xmlrpcConfgDb);
        Object[] l = { "license_plate","=",carCode };
        Object[] filters = { l };
        // filters.add(l);
        Object[] params = new Object[] { db, id, password, "fleet.vehicle",
                "search", filters };
        Object[] o = null;
        try {
            o = (Object[]) xmlrpc.execute("execute", params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        xmlrpcConfgDb = null;
        return o[0];
    }  
}
修改操作:

public void writeObject(int uid,String model,HashMap<String, Object> values,ArrayList ids) throws MalformedURLException {

            XmlRpcClient xmlrpc = new XmlRpcClient();

            XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();
            xmlrpcConfgDb.setEnabledForExceptions(true);
            xmlrpcConfgDb.setServerURL(new URL(
                    "http://"+appGlobal.getHostname()+":"+appGlobal.getPort()+"/xmlrpc/object"));

            xmlrpc.setConfig(xmlrpcConfgDb);
            String dbname = appGlobal.getDbname();
            String pwd = appGlobal.getPassword();   
            Object[] params = new Object[] { dbname, uid, pwd, model,
                    "write", ids,values };

            try {
                xmlrpc.execute("execute", params);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
}
总结:
在进行调用的时候,通常会遇到类型转化的错误,这是由于在调用方法的时候传输参数出现错误的情况,比如我们想对openerp里的fleet.vehicle数据表某个字段进行修改。那么这个时候我们需要知道要修改的行号,我们可能会传个int类型过去,但是在openerp端,write()方法中的参数里ids的类型却为List类型,所以这个时候就会报数据类型转化的错误。所以在进行调用的时候一定要弄清楚服务器端需要的参数类型是什么样的。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|技术支持|开发手册|Odoo中文网-远鼎旗下odoo培训网站 ( 苏ICP备15039516号 )

GMT+8, 2024-4-26 08:38 , Processed in 0.012319 second(s), 10 queries , Xcache On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表