I use and love the WordPress platform for this and several other blogs that I maintain. One of the many cool features of WordPress is that it supports XML-RPC. What does that mean? Well XML is… XML. It’s the extensible markup language and is basically just a way of formatting data in some meaningful way. RPC stands for remote procedure call. That is making calls to methods/procedures on a remote system. Put them together and you get the idea… making method/procedure calls on remote systems using XML to bundle up the method calls and returns.
Great,
I actually decided not to use any of the APIs mentioned above. Instead I went with the Redstone XML-RPC library. This API provides a layer of abstraction from all of the XML-RPC details that is nice and it also gives me the flexibility to implement any methods that I want. This will come in handy as you will see in a later post. For now though, how might I make a post using this API? Simple really. We’ll start out with the Blogger API method call. The method signature looks like this:
blogger.newPost(blog_ID, user_login, user_pass, content, publish)
The blog_ID is used for systems that maintain multiple blogs. There is actually a method we could call to get that piece of information called blogger.getUsersBlogs. Here’s a little secret though… WordPress doesn’t currently support this feature and is just hard-coded to be “1″. So for brevity sake I’ll just use that for now.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import redstone.xmlrpc.XmlRpcClient; public class XmlRpcPoster { public static void main( String[] args ) { // Check command-line arguments if( args.length < 3 ) { System.out.println( "Usage: java XmlRpcPoster <xml-rpc> <username> <password>" ); } else { // Get command-line arguments into variables String sXmlRpcURL = args[0]; String sUsername = args[1]; String sPassword = args[2]; // Hard-coded blog_ID int blog_ID = 1; // XML-RPC method String sXmlRpcMethod = "blogger.newPost"; // We'll hard-code our blog content for now as well String sContent = "Hello XML-RPC World!"; // You can specify whether or not you want the blog published immediately bool bPublish = true; // Try block try { // Create the XML-RPC client XmlRpcClient client = new XmlRpcClient( sXmlRpcURL ); // Make our method call Object token = client.invoke( sXmlRpcMethod, new Object[] { new Integer( blog_ID ), sUsername, sPassword, sContent, new Boolean( bPublish ) } ); // The return is a String containing the postID System.out.println( "Posted : " + (String)token ); } // Catch exceptions catch( Exception e ) { e.printStackTracke( System.err ); } } } } |
Wait a minute… there is no XML code anywhere in your example. Exactly! The Redstone API uses serializers to take the Object[] you provide and create the appropriate XML. Spiffy, eh? Not only that, but if the return type of the method call happens to be a struct, then the token will contain a Map that you can iterate over and retrieve values from.
I hope you enjoyed this little excursion into XML-RPC and Java. Stay tuned for my next post where I’ll be kicking this example up a notch to get more functionality and meta-data into our WordPress posting.




If I have a blog addresses http://amrishodiq.wordpress.com/
With your poster, what should I enter for the URL, is it http://amrishodiq.wordpress.com/, or http://amrishodiq.wordpress.com/xmlrpc.php?
Cause, I tried it then failed.
Here is the error report
redstone.xmlrpc.XmlRpcFault: Kombinasi login/kata sandi yang salah.
at redstone.xmlrpc.XmlRpcClient.handleResponse(Unknown Source)
at redstone.xmlrpc.XmlRpcClient.endCall(Unknown Source)
at redstone.xmlrpc.XmlRpcClient.invoke(Unknown Source)
at XmlRpcPoster.main(XmlRpcPoster.java:39)
That’s mean I enter wrong pair of username and password (Indonesian). But I really sure that I give the correct username and password.
Thanks for posting, Amri. I used the code in this article to try and recreate the problem and in fact I could not login either. There were also some errors inside the code itself. I wrote another follow-up article to this one however titled WordPress Posting Via Java – Part 2. This follow-up article has a download for a .java file that you can use instead of the copy/paste method here. Plus… the code in the follow-up article works! So head over to that article, grab the code and see if that solves the problem. Also, let me know how it turns out.
Thanks again!
Sir,
i browsed ure site,but i didnt find what i was looking for,
i trying to build a server and a client using netbeans 6.1. i have the neccessary redstone library files.
i somehow created a client but i cudnt create a server…………….
so, i need ure help.
Pls help me………………. i want the server code in another computer so that i can access the client from another……… i need ure great help.
Reply soon at librain.robin@gmail.com
Yes indeed. It works. Thanks a lot.