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:

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:

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:

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:

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

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

Then finally there is the method call:

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)

  • http://sudarmuthu.com/blog/ Sudar

    Thanks for sharing the code.

    I am going to convert this code to C# and PHP.


    Sudar

  • Heath

    Thanks for the code. It worked fine on my wordpress blog. However, it didn’t set the title.

    I’m running wordpress 2.3.1. In order for the title to work, I had to set my content to the following:

    This is my titleThis is the content of my blog post

    I did this and my title and content showed up in the blog appropriately.

  • Anupam

    Thanks for sharing this code and description. I guess this is only simplest googleable example to connect to WordPress from java [using redstone]

    Thanks a gallon.

  • http://masteringwave.com Daniel Graversen

    Hi,
    Thanks this works great.
    I was first using the version 1, and tried to solve the problem but without any luck.
    Daniel

  • http://www.techiepark.com/ TechiePark

    Thanks Franzone..!!! Gr8 work…keep it up.. it works for wordpress

  • RyanM

    Any ideas for what to do about a denial? I’m getting a 405 response code and it’s bloody frustrating. I’m trying to post to a http://blogname.wordpress.com/xmlrpc.php and every time the post request gets rejected. Do I need to do anything to enable remote posting to the blog from the web interface?

  • Silvia

    thank you so much for sharing this code. I’ve searched a lot for this, you saved my day!!!!!

  • Khoyendra

    Hi,
    I am trying to use this code but I have not found jar for this. I have searched in google and yahoo but at present I have not found jar for “redstone.xmlrpc.XmlRpcClient” class. I was got a jar for this class that is from comman apache but its not working.
    Please give a link were I can download this jar.
    Please reply on khoyendra@globussoft.com.
    Thanks

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

    Well, my friend, you didn’t search too terribly hard. It’s right here:
    http://xmlrpc.sourceforge.net/

  • Khoyendra

    Thank you very much, trust me i was search on google and yahoo for this. Thanks!!!

  • Julia

    Thank you for posting this code! Google App Engine seems to have issues with is own Blogger API, so this is a wonderful alternative.

    However, I’m having some issues. I’m getting some error with parsing. I chopped up and converted it to work in a servlet so later I can get it to post automatically. I have the xml-rpc URL, the username, and password hardcoded for now. There seems to be some issue with parsing. My two exceptions that got caught are…

    redstone.xmlrpc.XmlRpcException: The response could not be parsed.

    Caused by: java.io.IOException: Could not fetch URL: http://blogname.wordpress.com/xmlrpc.php

    I would appreciate an email, but I’ll check regularly too. Thanks!

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

    Well, it looks to me like your code cannot reach the URL you are passing to it (i.e. “Could not fetch URL”). So I’d make sure you can reach the URL it is reading from. You could try loading up the URL in a web browser if you are running the code locally. If you are running it remotely on a server you could try logging into the server and doing a curl or wget to see if it can be reached.

  • Julia

    Forgive me, for this is pretty much my first time working with XML-RPC. Is the URL supposed print “XML-RPC server accepts POST requests only.”? I had that URL open with that line printed and tried to run, but it still has the same error.

    Also, I forgot to mention. The part of the code in the error stack that shows up points to the the method call
    Object token = client.invoke( sXmlRpcMethod, new Object[] { new Integer( blog_ID ), sUsername, sPassword, hmContent, new Boolean( bPublish ) } );

    Sorry for being so problematic!

  • mxb

    Hi,

    Please can you make the code available again as the link goes nowhere. Also can you tell me if this technique works find with Drupal and Joomla.

    Thanks,

    Mush

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

    Thanks for pointing out the broken link; it has been fixed and should get you the code.

  • Vishal

    Hey Thanks for the quick introduction to Redstone library. I am trying to post the content with a different author than the login user sUsername. I expect ‘author’ property should do that but it is not. Can you please suggest.

  • Vishal

    Well.. There is a slight change in the parameter list of the struct
    instead of ‘author’ it is ‘wp_author_id’. you can get the author_id from wp.getAuthors().

    more details are available at C:\wamp\www\wordpress\wp-includes\class-wp-xmlrpc-server.php

  • Estelle

    Thanks for the example, it sure got me started!

    Did you have any luck with uploading media? I’m trying to post jpeg images, and they do get posted to my wordpress site, but the image is corrupted.

    Maybe the base64 is not decoded right? I tried a couple libraries for base64, tried url-safe and non-url-safe…

    I’m using this content hashmap :

    content.put(“name”, “imagename.jpeg”);
    content.put(“type”, “image/jpeg”);
    content.put(“overwrite”, true);
    File mediaObject = new File(“path/to/imagename.jpeg”);
    content.put(“bits”, Base64.encodeBase64String(mediaObject.getBytes()));

    If you made it work I’d be very interested in a code sample.

    Thanks!

  • http://knowledgebyte.in Inder Bagga

    Hi there! Mr. Jonathan, thanks for sharing code. It would be awesome if you also write something like to call get methods getPage etc. to read using xml rpc …thanks a ton once again !!

  • Derek Medina

    Thank you so much for this example!!!!!!