September 15, 2009

Java XML-RPC - Example

« Spring Mail Example | Main | runtime replacement of jar file »

Historically, the XML-RPC protocol represents the forerunner of SOAP. It uses HTTP as the transport layer and XML as the data layer. In Java you can use the XmlRpcClient class from Apache WS to develop XML-RPC stubs.

The Metablog API describes a XML-RPC interface for remote publishing of blog enties. The following example shows how to add remote a new entry to your blog.

XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
//url for example http://developers-blog.org/xmlrpc
config.setServerURL(new URL(url));
config.setEnabledForExtensions(true);
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
Map m = new HashMap();
m.put("title", "Hello Entry");
m.put("link", "http://developers-blog.org");
m.put("description", "Blabla blub");
Object[] params = new Object[]{"default", username, password, m, true};
String ret = (String) client.execute("metaWeblog.newPost", params);
Raw XML-RPC request:
<methodCall>
    <methodName>metaWeblog.newPost</methodName>
    <params>
        <param><value><string>BLOG_ID</string></value></param>
        <param><value><string>USERNAME</string></value></param>
        <param><value><string>PASSWORD</string></value></param>
        <param>
            <value>
                <struct>
                    <member>
                        <name>title</name>
                        <value>
                            <string>Hello Entry</string>
                        </value>
                    </member>
                    <member>
                        <name>description</name>
                        <value>
                            <string>Blabla blub</string>
                        </value>
                    </member>
                    <member>
                        <name>link</name>
                        <value>
                            <string>http://developers-blog.org</string>
                        </value>
                    </member>
                </struct>
            </value>
        </param>
    </params>
</methodCall>

Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 2:41 AM in Uncategorized

 

[Trackback URL for this entry]

Comment: Yusuf at Mi, 21 Apr 12:21 PM

I've got the same code, however, my structs not include the types of the values.

Yours:
<member>
<name>description</name>
<value>
<string>Blabla blub</string>
</value>
</member>

Mine:
<member>
<name>description</name>
<value>
Blabla blub
</value>
</member>

Why?

Comment: Andreas Fischer at Fr, 19 Aug 8:19 AM

same for me. What's wrong - is it the releas-version of XmlRpc?

Your comment:

(not displayed)
 
 
 

Live Comment Preview: