Openerp-java Call Xml-rpc Interface Example (Examples for calling XML-RPC interfaces by Java)

Source: Internet
Author: User

The official website does not give examples of Java calls to create, SEARCH, write, etc. xml-rpc interfaces, to add this.

There is no examples on the official site for the XML-RPC operation interfaces in 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 XmlRpcCo        Nfiglogin = 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 = Entry.getvalue ();        Object[] Filter = new Object[]{key, "=", value};        Filterlist.add (filter);    } return Filterlist.toarray (); } public Integer Create (String object, map<string, Object> valueMap) throws Malformedurlexception, Xmlrpcexceptio        n {xmlrpcclientconfig executionconfig = Getexecutionconfig ();        Client.setconfig (Executionconfig);        object[] params = new object[]{instance, uid, password, Object, CREATE, valueMap};        Object result = Client.execute ("Execute", params);            If (result instanceof Integer) {int id = (Integer) result;        return ID;    } return-1; } public Boolean unlink (String object, integer[] IDs) throws Malformedurlexception, xmlrpcexception {Xmlrpcclie        Ntconfig executionconfig = Getexecutionconfig ();        Client.setconfig (Executionconfig);        object[] params = new object[]{instance, uid, password, Object, UNLINK, IDS}; Object result = Client.execute ("Execute", paraMS);        if (Result instanceof Boolean) {return (Boolean) result;    } return null; } public Boolean Write (String object, integer[] IDs, map<string, object> valueMap) throws Malformedurlexception,        xmlrpcexception {Xmlrpcclientconfig executionconfig = Getexecutionconfig ();        Client.setconfig (Executionconfig);        object[] params = new object[]{instance, uid, password, Object, WRITE, IDs, ValueMap, null};        Object result = Client.execute ("Execute", params);        if (Result instanceof Boolean) {return (Boolean) result;    } return null; } public integer[] Search (String object, map<string, Object> filtermap) throws Malformedurlexception, Xmlrpcexcep        tion {xmlrpcclientconfig executionconfig = Getexecutionconfig ();        Client.setconfig (Executionconfig);        Object[] Filters = getsearchfilters (FILTERMAP); object[] params = new object[]{instance, uid, password, Object, SEARCH, filters, 0, 0, null};        Object result = Client.execute ("Execute", params);            If (result instanceof object[]) {object[] Resulta = (object[]) result;            integer[] ids = new Integer[resulta.length];                for (int i = 0; i < resulta.length; i++) {Object o = resulta[i];                int id = (Integer) o;            IDS[I] = ID;        } return IDs;    } return null; } public list<map<string, object>> read (String Object, integer[] IDs, string[] fields) throws Malformedurle        Xception, xmlrpcexception {xmlrpcclientconfig executionconfig = Getexecutionconfig ();        Client.setconfig (Executionconfig);        object[] params = new object[]{instance, uid, password, Object, READ, IDs, fields, null};        Object result = Client.execute ("Execute", params);        List<map<string, object>> records = new arraylist<map<string, object>> (); If (result instanceof object[]) {           For (Object row: (object[]) result) {map<string, object> Map = (hashmap<string, objec                t>) Row;            Records.add (map);        } return records;    } return null;        } private static String getarraystring (object[] array) {StringBuilder sb = new StringBuilder ();        Sb.append ("[");            for (Object Element:array) {if (Sb.length () > 1) {sb.append (", \ T");        } sb.append (Element);        } sb.append ("]");    return sb.tostring (); } public static void Main (string[] args) throws Nosuchfieldexception, Illegalaccessexception, Malformedurlexception, Xm        lrpcexception {String host = "127.0.0.1";        int port = 8069;        String instance = "20140803";        String username = "admin";        String Password = "admin";        String object = "Test";      Cmpclient client = new Cmpclient (host, port, instance, username, password);  Test Connect () int uid = Client.connect ();            if (UID < 0) {System.err.println ("Bad username or password!");        Return        }else {System.out.println ("uid:\t" + UID);        }//Test Create () Map createvalues = new hashmap<string, object> ();        Createvalues.put ("name", "Test");        Integer id = Client.create (object, createvalues);        if (ID < 0) {System.err.println ("Creation Failed");        }else {System.out.println ("Created id:\t" + ID);        }//Test unlink () integer[] unlinkids = new Integer[]{1};        Boolean unlinked = Client.unlink (object, unlinkids);        System.out.println ("unlinked:\t" + unlinked);        Test write () integer[] writeids = new Integer[]{1};        Map writevalues = new hashmap<string, object> ();        Writevalues.put ("name", "Test1");        Boolean writen = Client.write (object, Writeids, writevalues); SysteM.out.println ("writen:\t" + writen);        Test search () Map filtermap = new hashmap<string, object> ();        Filtermap.put ("name", "Test1");        integer[] Searchedids = Client.search (object, Filtermap);        System.out.println ("searched ids:\t" + getarraystring (searchedids));        Test read () integer[] readids = new Integer[]{1};        string[] ReadFields = new string[]{"name", "description"};        List<map<string, object>> records = Client.read (Object, Readids, readfields);        System.out.println ("Read:");        For (map<string, object> map:records) {System.out.println (MAP); }    }}

Jar packages to use: 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 Call Xml-rpc Interface Example (Examples for calling XML-RPC interfaces by Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.