WordPress Posting Via Java – Part 2

|

In the first part of this series titled “Would You Like To Post To WordPress From Java?” I introduced you (hopefully) to the XML-RPC mechanism that WordPress implements. I also showed how it is quite simple to post to a WordPress blog using Java and the Redstone XML-RPC API. In this installation I’d like to expand the example a bit further.

First, however I’d like to apologize for posting non-working code. 🙁 I posted the example without actually compiling and/or testing it. If you downloaded and fixed the errors to get it to compile you also probably noticed that the method would not successfully login to your blog. Doh! I’ll be fixing those errors in the next version of the file, so hopefully you will walk away with something you can actually modify to your liking… and use.

Now, how about that last example? Did you notice anything lacking in the data that we posted. If you recall the method signature looks like this:

blogger.newPost(blog_ID, user_login, user_pass, content, publish)

This is actually part of the problem with the first implementation. Looking into the xmlrpc.php file, the method starts looking for the blog_ID at array index 1… not zero. So the method signature should actually be like this:

blogger.newPost(emptyData, blog_ID, user_login, user_pass, content, publish)

I’m not sure what emptyData is supposed to be or even why it is there, but I just posted an empty string here and everything was great. The method doesn’t actually ever use that argument… odd.

Ok, back to the initial question… do you notice anything lacking? Sure, a lot of things are missing. Mainly and most blaring is the lack of a title. In the above and previous example all we are posting is the content. That’s no good. I would like to at a minimum be able to specify the title of my post. For that we will take advantage of the metaWeblog API calls. There is a similar function with the following signature:

metaWeblog.newPost(blog_ID, user_login, user_pass, content_struct, publish)

First, this method does NOT contain the empty data argument. Secondly you’ll notice that instead of passing a string for content, we are passing a struct. So what type of data might this struct contain? It contains: title, link, description, author, category, comments, enclosure, guid, pubDate and source. Wow, that’s a lot more information! I won’t be covering all of these different data elements, but you can read more about them on the RSS 2.0 Specification on the Harvard Law IT Page. There are actually a few more elements than listed here which we will make use of in an upcoming post. 🙂 For now, however I am content to just have the title.

We’ll need to change the method we are calling:

// XML-RPC method
String sXmlRpcMethod = "metaWeblog.newPost";

We’ll need to create our content struct, which will be created using a

java.util.HashMap

. Don’t worry, the Redstone serializers will convert this into the XML that we need. Nice!

// Create our content struct
HashMap hmContent = new HashMap();
hmContent.put("title", sTitle);
hmContent.put("description", sContent);

Then finally there is the method call:

// Make our method call
Object token = client.invoke(sXmlRpcMethod,
                             new Object[]
                             {
                               new Integer( blog_ID ),
                               sUsername,
                               sPassword,
                               hmContent,
                               new Boolean( bPublish )
                             });

That’s it! Just a few simple changes to our original example (plus the bug fixes) and we now have a title. You can also play around with adding other elements such as a Category. Here is the working file if you would like to download the source.

XmlRpcPoster.java (1.68 KB)