java xmlrpc2.0 实现_Java xml rpc 访问openerp的详细例子

群里面问了一大转,还是没有找到java怎么调用xml的例子....

无语,只能自己参考doc.openerp.com研究了。

这里面只有java xml rpc的login和list db例子,但是重要的对象读写操作却没有涉及到。

分享一下我测试的例子,javaer不用再碰墙了...

简单起见,这里定义了自己的对象x_test,只有两个属性,x_name和x_desc,均为char

简单起见,直接贴代码了...

更新说明一下,这里需要使用apache的xml rpc包,请到apache的网站下载,加上就ok了。

import java.net.MalformedURLException;

import java.net.URL;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashMap;

import java.util.List;

import java.util.Vector;

import org.apache.xmlrpc.XmlRpcException;

import org.apache.xmlrpc.client.XmlRpcClient;

import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

public class xmltest {

/**

* @param args

* @throws MalformedURLException

*/

public static void main(String[] args) throws MalformedURLException {

// TODO Auto-generated method stub

// getDatabaseList("127.0.0.1",8069);

// createObject();

// searchObject();

// readData();

readView();

}

public static Vector getDatabaseList(String host, int port)

throws MalformedURLException {

XmlRpcClient xmlrpcDb = new XmlRpcClient();

XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();

xmlrpcConfgDb.setEnabledForExceptions(true);

xmlrpcConfgDb.setServerURL(new URL("http", host, port, "/xmlrpc/db"));

xmlrpcDb.setConfig(xmlrpcConfgDb);

Vector res = new Vector();

try {

Vector params = new Vector();

Object result = xmlrpcDb.execute("list", params);

Object[] a = (Object[]) result;

for (int i = 0; i < a.length; i++) {

res.addElement((String) a[i]);

System.out.println((String) a[i]);

}

} catch (Exception e) {

e.printStackTrace();

}

return res;

}

public int Connect(String host, int port, String tinydb, String login,

String password) throws Exception {

XmlRpcClient xmlrpclogin = new XmlRpcClient();

XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();

xmlrpcConfigLogin.setEnabledForExceptions(true);

xmlrpcConfigLogin.setServerURL(new URL("http", host, port,

"/xmlrpc/common"));

xmlrpclogin.setConfig(xmlrpcConfigLogin);

try {

Object[] params = new Object[] { tinydb, login, password };

Object id = xmlrpclogin.execute("login", params);

if (id instanceof Integer)

return (Integer) id;

return -1;

} catch (Exception e) {

e.printStackTrace();

}

return -1;

}

public static void createObject() throws MalformedURLException {

XmlRpcClient xmlrpc = new XmlRpcClient();

XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();

xmlrpcConfgDb.setEnabledForExceptions(true);

xmlrpcConfgDb.setServerURL(new URL(

"http://localhost:8069/xmlrpc/object"));

xmlrpc.setConfig(xmlrpcConfgDb);

String dbname = "openerp";

int uid = 1;

String pwd = "000000";

String model = "x_test";

HashMap values = new HashMap();

values.put("x_name", "Monsieur");

// values.put("x_desc", "bbbb");

// Object[] params = new Object[] { dbname, uid, pwd, model,"create",

// values };

Object[] params = new Object[] { "openerp", 1, "000000", "x_test",

"create", values };

try {

xmlrpc.execute("execute", params);

} catch (Exception e) {

e.printStackTrace();

}

}

public static void searchObject() throws MalformedURLException {

XmlRpcClient xmlrpc = new XmlRpcClient();

XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();

xmlrpcConfgDb.setEnabledForExceptions(true);

xmlrpcConfgDb.setServerURL(new URL(

"http://localhost:8069/xmlrpc/object"));

xmlrpc.setConfig(xmlrpcConfgDb);

String dbname = "openerp";

int uid = 1;

String pwd = "000000";

String model = "x_test";

/*

* ArrayList l = new ArrayList(); l.add("x_name");

* l.add("="); l.add("a"); ArrayList filters = new ArrayList();

*/

Object[] l = { "x_name", "=", "a" };

Object[] filters = { l };

// filters.add(l);

Object[] params = new Object[] { "openerp", 1, "000000", "x_test",

"search", filters };

try {

Object[] o = (Object[]) xmlrpc.execute("execute", params);

System.out.println("finished");

// System.out.println(o.length);

for (Object obj : o) {

System.out.println(obj);

}

} catch (Exception e) {

e.printStackTrace();

}

}

public static void readData() throws MalformedURLException {

Object[] ids = { 2, 3 };

Object[] fields = { "x_name", "x_desc" };

XmlRpcClient xmlrpc = new XmlRpcClient();

XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();

xmlrpcConfgDb.setEnabledForExceptions(true);

xmlrpcConfgDb.setServerURL(new URL(

"http://localhost:8069/xmlrpc/object"));

xmlrpc.setConfig(xmlrpcConfgDb);

String dbname = "openerp";

int uid = 1;

String pwd = "000000";

String model = "x_test";

Object[] params = new Object[] { "openerp", 1, "000000", "x_test",

"read", ids, fields };

try {

Object[] o = (Object[]) xmlrpc.execute("execute", params);

System.out.println("finished");

// System.out.println(o.length);

for (Object obj : o) {

System.out.println(obj);

}

} catch (Exception e) {

e.printStackTrace();

}

}

public static void readView() throws MalformedURLException {

Object[] ids = { 1276 };

Object[] fields = { "arch" };

XmlRpcClient xmlrpc = new XmlRpcClient();

XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();

xmlrpcConfgDb.setEnabledForExceptions(true);

xmlrpcConfgDb.setServerURL(new URL(

"http://localhost:8069/xmlrpc/object"));

xmlrpc.setConfig(xmlrpcConfgDb);

String dbname = "openerp";

int uid = 1;

String pwd = "000000";

String model = "x_test";

Object[] params = new Object[] { "openerp", 1, "000000", "ir.ui.view",

"read", ids, fields };

try {

Object[] o = (Object[]) xmlrpc.execute("execute", params);

// System.out.println("finished");

System.out.println(o.length);

for (Object obj : o) {

//System.out.println(obj);

HashMap h = (HashMap)obj;

//System.out.println(h.keySet().size());

System.out.println("the id is:");

System.out.println(h.get("id"));

System.out.println("the arch is:");

System.out.println(h.get("arch"));

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值