Would You Like To Post To WordPress From Java?

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, now I have to write some sort of custom program to build an XML formatted message and submit it to WordPress. Then I have to have the same program be able to parse and decipher the return XML format. Well, no you don’t! There are already libraries that will do this for you. WordPress supports the Blogger API, the metaWeblog API and the MoveableType API. These are blog specific APIs that use XML-RPC to communicate. You could very easily download one of these, read the documentation and become productive with Java in no time at all. That was actually my first step. But wait… the APIs don’t support all of the same methods. Which one should I use?

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.

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.

  • Pingback: WordPress Posting Via Java - Part 2 | franzone.com

  • Pingback: XML-RPC Java Sample: Sorry About The Formatting | franzone.com

  • http://amrishodiq.wordpress.com Amri Shodiq

    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.

  • http://amrishodiq.wordpress.com Amri Shodiq

    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.

  • http://www.franzone.com Jonathan Franzone

    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!

  • Robin

    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

  • http://amrishodiq.wordpress.com Amri Shodiq

    Yes indeed. It works. Thanks a lot.