Odoo中文社区可以通过以下两个域名访问:shine-it.net , odoo.net.cn

由于系统升迁的原因,本论坛部分较早期的内容存在格式和链接损坏失效的问题,并非本论坛系统本身的缺陷,望谅解

本社区没有维护任何QQ群讨论组,任何与本社区同名的QQ群讨论组的言论与本社区无关!

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

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了。

    <br />import java.net.MalformedURLException;<br />import java.net.URL;<br />import java.util.ArrayList;<br />import java.util.Arrays;<br />import java.util.HashMap;<br />import java.util.List;<br />import java.util.Vector;<br /><br />import org.apache.xmlrpc.XmlRpcException;<br />import org.apache.xmlrpc.client.XmlRpcClient;<br />import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;<br /><br /><br />public class xmltest {<br /><br />	/**<br />	 * @param args<br />	 * @throws MalformedURLException<br />	 */<br />	public static void main(String&#91;] args) throws MalformedURLException {<br />		// TODO Auto-generated method stub<br />		// getDatabaseList(&quot;127.0.0.1&quot;,8069);<br /><br />		// createObject();<br />		// searchObject();<br />		// readData();<br />		readView();<br />	}<br /><br />	public static Vector&lt;String&gt; getDatabaseList(String host, int port)<br />			throws MalformedURLException {<br />		XmlRpcClient xmlrpcDb = new XmlRpcClient();<br /><br />		XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();<br />		xmlrpcConfgDb.setEnabledForExceptions(true);<br />		xmlrpcConfgDb.setServerURL(new URL(&quot;http&quot;, host, port, &quot;/xmlrpc/db&quot;));<br /><br />		xmlrpcDb.setConfig(xmlrpcConfgDb);<br />		Vector&lt;String&gt; res = new Vector&lt;String&gt;();<br />		try {<br />			Vector&lt;Object&gt; params = new Vector&lt;Object&gt;();<br />			Object result = xmlrpcDb.execute(&quot;list&quot;, params);<br />			Object&#91;] a = (Object&#91;]) result;<br /><br />			for (int i = 0; i &lt; a.length; i++) {<br />				res.addElement((String) a[i]);<br />				System.out.println((String) a[i]);<br />			}<br /><br />		} catch (Exception e) {<br />			e.printStackTrace();<br />		}<br />		return res;<br />	}<br /><br />	public int Connect(String host, int port, String tinydb, String login,<br />			String password) throws Exception {<br />		XmlRpcClient xmlrpclogin = new XmlRpcClient();<br /><br />		XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();<br /><br />		xmlrpcConfigLogin.setEnabledForExceptions(true);<br />		xmlrpcConfigLogin.setServerURL(new URL(&quot;http&quot;, host, port,<br />				&quot;/xmlrpc/common&quot;));<br /><br />		xmlrpclogin.setConfig(xmlrpcConfigLogin);<br /><br />		try {<br />			Object&#91;] params = new Object&#91;] { tinydb, login, password };<br />			Object id = xmlrpclogin.execute(&quot;login&quot;, params);<br />			if (id instanceof Integer)<br />				return (Integer) id;<br />			return -1;<br />		} catch (Exception e) {<br />			e.printStackTrace();<br />		}<br /><br />		return -1;<br />	}<br /><br />	public static void createObject() throws MalformedURLException {<br />		XmlRpcClient xmlrpc = new XmlRpcClient();<br /><br />		XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();<br />		xmlrpcConfgDb.setEnabledForExceptions(true);<br />		xmlrpcConfgDb.setServerURL(new URL(<br />				&quot;http://localhost:8069/xmlrpc/object&quot;));<br /><br />		xmlrpc.setConfig(xmlrpcConfgDb);<br /><br />		String dbname = &quot;openerp&quot;;<br />		int uid = 1;<br />		String pwd = &quot;000000&quot;;<br />		String model = &quot;x_test&quot;;<br /><br />		HashMap&lt;String, Object&gt; values = new HashMap&lt;String, Object&gt;();<br />		values.put(&quot;x_name&quot;, &quot;Monsieur&quot;);<br />		// values.put(&quot;x_desc&quot;, &quot;bbbb&quot;);<br /><br />		// Object&#91;] params = new Object&#91;] { dbname, uid, pwd, model,&quot;create&quot;,<br />		// values };<br /><br />		Object&#91;] params = new Object&#91;] { &quot;openerp&quot;, 1, &quot;000000&quot;, &quot;x_test&quot;,<br />				&quot;create&quot;, values };<br /><br />		try {<br />			xmlrpc.execute(&quot;execute&quot;, params);<br />		} catch (Exception e) {<br />			e.printStackTrace();<br />		}<br />	}<br /><br />	public static void searchObject() throws MalformedURLException {<br />		XmlRpcClient xmlrpc = new XmlRpcClient();<br /><br />		XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();<br />		xmlrpcConfgDb.setEnabledForExceptions(true);<br />		xmlrpcConfgDb.setServerURL(new URL(<br />				&quot;http://localhost:8069/xmlrpc/object&quot;));<br /><br />		xmlrpc.setConfig(xmlrpcConfgDb);<br /><br />		String dbname = &quot;openerp&quot;;<br />		int uid = 1;<br />		String pwd = &quot;000000&quot;;<br />		String model = &quot;x_test&quot;;<br />		/*<br />		 * ArrayList&lt;String&gt; l = new ArrayList&lt;String&gt;(); l.add(&quot;x_name&quot;);<br />		 * l.add(&quot;=&quot;); l.add(&quot;a&quot;); ArrayList filters = new ArrayList();<br />		 */<br />		Object&#91;] l = { &quot;x_name&quot;, &quot;=&quot;, &quot;a&quot; };<br />		Object&#91;] filters = { l };<br />		// filters.add(l);<br />		Object&#91;] params = new Object&#91;] { &quot;openerp&quot;, 1, &quot;000000&quot;, &quot;x_test&quot;,<br />				&quot;search&quot;, filters };<br /><br />		try {<br /><br />			Object&#91;] o = (Object&#91;]) xmlrpc.execute(&quot;execute&quot;, params);<br /><br />			System.out.println(&quot;finished&quot;);<br />			// System.out.println(o.length);<br />			for (Object obj : o) {<br />				System.out.println(obj);<br />			}<br />		} catch (Exception e) {<br />			e.printStackTrace();<br /><br />		}<br />	}<br /><br />	public static void readData() throws MalformedURLException {<br />		Object&#91;] ids = { 2, 3 };<br />		Object&#91;] fields = { &quot;x_name&quot;, &quot;x_desc&quot; };<br /><br />		XmlRpcClient xmlrpc = new XmlRpcClient();<br /><br />		XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();<br />		xmlrpcConfgDb.setEnabledForExceptions(true);<br />		xmlrpcConfgDb.setServerURL(new URL(<br />				&quot;http://localhost:8069/xmlrpc/object&quot;));<br /><br />		xmlrpc.setConfig(xmlrpcConfgDb);<br /><br />		String dbname = &quot;openerp&quot;;<br />		int uid = 1;<br />		String pwd = &quot;000000&quot;;<br />		String model = &quot;x_test&quot;;<br /><br />		Object&#91;] params = new Object&#91;] { &quot;openerp&quot;, 1, &quot;000000&quot;, &quot;x_test&quot;,<br />				&quot;read&quot;, ids, fields };<br /><br />		try {<br /><br />			Object&#91;] o = (Object&#91;]) xmlrpc.execute(&quot;execute&quot;, params);<br /><br />			System.out.println(&quot;finished&quot;);<br />			// System.out.println(o.length);<br />			for (Object obj : o) {<br />				System.out.println(obj);<br />			}<br />		} catch (Exception e) {<br />			e.printStackTrace();<br /><br />		}<br />	}<br /><br />	public static void readView() throws MalformedURLException {<br />		Object&#91;] ids = { 1276 };<br />		Object&#91;] fields = { &quot;arch&quot; };<br /><br />		XmlRpcClient xmlrpc = new XmlRpcClient();<br /><br />		XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();<br />		xmlrpcConfgDb.setEnabledForExceptions(true);<br />		xmlrpcConfgDb.setServerURL(new URL(<br />				&quot;http://localhost:8069/xmlrpc/object&quot;));<br /><br />		xmlrpc.setConfig(xmlrpcConfgDb);<br /><br />		String dbname = &quot;openerp&quot;;<br />		int uid = 1;<br />		String pwd = &quot;000000&quot;;<br />		String model = &quot;x_test&quot;;<br /><br />		Object&#91;] params = new Object&#91;] { &quot;openerp&quot;, 1, &quot;000000&quot;, &quot;ir.ui.view&quot;,<br />				&quot;read&quot;, ids, fields };<br /><br />		try {<br /><br />			Object&#91;] o = (Object&#91;]) xmlrpc.execute(&quot;execute&quot;, params);<br /><br />			// System.out.println(&quot;finished&quot;);<br />			System.out.println(o.length);<br />			for (Object obj : o) {<br />			//	System.out.println(obj);<br /><br />				HashMap h = (HashMap)obj;<br />				//System.out.println(h.keySet().size());<br />				System.out.println(&quot;the id is:&quot;);<br />				System.out.println(h.get(&quot;id&quot;));<br />				System.out.println(&quot;the arch is:&quot;);<br />				System.out.println(h.get(&quot;arch&quot;));<br />			}<br />		} catch (Exception e) {<br />			e.printStackTrace();<br /><br />		}<br />	}<br /><br />}<br />
    



    i'm '深圳-steven' ...



  • 坐自己沙发...
    广告招租 😉



  • 谢谢分享



  • 哈,经典。 为很多android + openerp整合的人铺路了


Log in to reply