MetaWeblog API in Java at indiWiz.com
MetaWeblog API in Java
Recently we were assigned with the task of writing code to remotely insert an entry to a blog. And we had to select the Blogging software. So we started with the search for a blogging software which exposed its API. To our amazement, we found XML-RPC ruling the blogging API world. And one software independent standard for it is the MetaWeblog API.
After selecting the API, we had to find a software supporting this API. This assignment was for a demo we were to show. So we wanted a minimal blogging software with no configuration. We found Pebble (we used version 2.3.1). This software is cool. Installation is breeze, just put the WAR file in an application server, and you are ready to use it!
Now came the implementation part. I assumed the existence of a Java library implementing this MetaWeblog API. But did not find any. So the next obvious step: use some XML-RPC library in Java to implement the call. I had some bad opinion about Apache’s XML-RPC library. I had used it long back ago, and I hated the way it was implemented. Since then I have been using Python to access XML-RPC services. But, this time, the job required use of Java.
So we ventured to the next step: finding an alternative OpenSource Java XML-RPC library. We found RoX. We were happy because RoX was born out of frustration of using Apache’s XML-RPC engine. We implemented it. And RoX did not work for Pebble (still not able to figure out if it was a mistake on our part, or RoX really did have a bug). So switched to Apache (version 3.1.1). The 3.x API seems to have been simplified. So finally we wrote:
12345678910111213141516171819202122import
org.apache.xmlrpc.XmlRpcException;
import
org.apache.xmlrpc.client.XmlRpcClient;
import
org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
...
XmlRpcClientConfigImpl config =
new
XmlRpcClientConfigImpl();
XmlRpcClient client =
new
XmlRpcClient();
client.setConfig(config);
// Params needed: blogid, username, password, struct, publish
// First create the fourth parameter, struct:
Map<string, String> m =
new
HashMap<string, String>();
m.put(
"title"
,
"Hello World "
+ Calendar.getInstance().toString());
m.put(
"description"
,
"This is the content of the post!"
);
Object[] params =
new
Object[]{
"default"
,
"username"
,
"password"
, m,
true
};
String ret = (String) client.execute(
"metaWeblog.newPost"
, params);
System.out.println(ret);
And then we delivered our demo!