首页 > 代码库 > OpenERP-Java调用XML-RPC接口示例(Examples for calling XML-RPC interfaces by Java)
OpenERP-Java调用XML-RPC接口示例(Examples for calling XML-RPC interfaces by Java)
官网没有给出CREATE、SEARCH、WRITE等XML-RPC接口的Java 调用示例,在此补充一下。
There is no examples on the official site for the XML-RPC operation interfaces for Java, so I posted my code here.
/** * Created by kylin on 14-9-22. */ import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfig; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CmpClient { private static final String LOGIN_PATH = "/xmlrpc/common"; private static final String EXECUTION_PATH = "/xmlrpc/object"; private static final String CREATE = "create"; private static final String UNLINK = "unlink"; private static final Object WRITE = "write"; private static final String SEARCH = "search"; private static final Object READ = "read"; private String protocol = "http"; private String host = "127.0.0.1"; private int port = 8069; private String instance = "20140803"; private String username = "admin"; private String password = "admin"; private int uid = -1; public CmpClient(String host, int port, String instance, String login, String password) { this.host = host; this.port = port; this.instance = instance; this.username = login; this.password = password; } public CmpClient(){ } XmlRpcClient client = new XmlRpcClient(); private XmlRpcClientConfig getLoginConfig() throws MalformedURLException { XmlRpcClientConfigImpl xmlRpcConfigLogin = new XmlRpcClientConfigImpl(); xmlRpcConfigLogin.setEnabledForExtensions(true); URL loginURL = new URL(protocol, host, port, LOGIN_PATH); xmlRpcConfigLogin.setServerURL(loginURL); return xmlRpcConfigLogin; } private XmlRpcClientConfig getExecutionConfig() throws MalformedURLException { XmlRpcClientConfigImpl xmlRpcConfigLogin = new XmlRpcClientConfigImpl(); xmlRpcConfigLogin.setEnabledForExtensions(true); URL loginURL = new URL(protocol, host, port, EXECUTION_PATH); xmlRpcConfigLogin.setServerURL(loginURL); return xmlRpcConfigLogin; } public int connect() { try { XmlRpcClientConfig loginConfig = getLoginConfig(); client.setConfig(loginConfig); Object[] params = new Object[]{instance, username, password}; Object id = client.execute("login", params); if (id instanceof Integer) { uid = (Integer) id; return uid; } return -1; } catch (XmlRpcException e) { e.printStackTrace(); return -2; } catch (Exception e) { e.printStackTrace(); return -3; } } private Object[] getSearchFilters(Map<String, Object> filterMap){ List<Object[]> filterList = new ArrayList<Object[]>(); for(Map.Entry<String, Object> entry : filterMap.entrySet()){ String key = entry.getKey(); Object value = http://www.mamicode.com/entry.getValue();>
需要使用的jar包:xmlrpc-client-3.1.3.jar, xmlrpc-common-3.1.3.jar, ws-commons-util-1.0.2.jarThe jar files required:xmlrpc-client-3.1.3.jar, xmlrpc-common-3.1.3.jar, ws-commons-util-1.0.2.jar
OpenERP-Java调用XML-RPC接口示例(Examples for calling XML-RPC interfaces by Java)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。