首页 > 代码库 > 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.jar

The 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)