浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

MetaWeblog API in Java at indiWiz.com

MetaWeblog API in Java

with one comment

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
 
...
 
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://localhost:8080/pebble/xmlrpc/"));
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("link", "http://www.indiwiz.com/");
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!

posted on 2012-11-25 09:21  lexus  阅读(617)  评论(0编辑  收藏  举报