Odoo中文网|Odoo实施培训
标题: 网上的OpenERP-XML-RPC的Jar包应用 [打印本页]
作者: 店小2 时间: 2016-10-12 22:35
标题: 网上的OpenERP-XML-RPC的Jar包应用
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类型,所以这个时候就会报数据类型转化的错误。所以在进行调用的时候一定要弄清楚服务器端需要的参数类型是什么样的。
欢迎光临 Odoo中文网|Odoo实施培训 (http://www.chinaodoo.net/) |
Powered by Discuz! X3.2 |