MetaweblogPoster用来给支持Metaweblog api的博客发送博客文章.目前只实现了获取博文分类和发送博客功能.
1: package com.leipei.blogutil;
2:
3: import java.net.MalformedURLException;
4: import java.net.URL;
5: import java.util.ArrayList;
6: import java.util.HashMap;
7: import java.util.Map;
8:
9: import org.apache.xmlrpc.XmlRpcException;
10: import org.apache.xmlrpc.client.XmlRpcClient;
11: import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
12:
13: public class MetaweblogPoster {
14:
15: private URL blogUrl;
16: private XmlRpcClient blogClient;
17:
18: public MetaweblogPoster(URL blogUrl) {
19: this.blogUrl = blogUrl;
20: this.initBlogClient();
21: }
22:
23: public URL getBlogUrl() {
24: return blogUrl;
25: }
26:
27: /*
28: * 初始化博客发送客户端
29: */
30: private void initBlogClient() {
31: if (this.blogUrl != null) {
32: XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
33: config.setServerURL(this.blogUrl);
34: this.blogClient = new XmlRpcClient();
35: blogClient.setConfig(config);
36: }
37: }
38:
39: /*
40: * @name 发博文时需要的用户名
41: *
42: * @password 发博文时需要的密码
43: *
44: * @blogUrl 发博文时对应的metaweblog url
45: *
46: * @blog 要发送的博文对象,它存储了博文的标题,分类,标签,内容等信息
47: */
48: public String post(String name, String password, Blog blog) {
49: try {
50:
51: // Set up parameters required by newPost method
52: Map<String, Object> post = new HashMap<String, Object>();
53: post.put("title", blog.getTitle());// 标题
54: post.put("categories", blog.getCategory());// 分类
55: post.put("description", blog.getContent());// 内容
56: Object[] params = new Object[] { "default", name, password, post,
57: Boolean.TRUE };
58:
59: // 发布新博文
60: String result = (String) this.blogClient.execute(
61: "metaWeblog.newPost", params);
62: System.out.println(" Created with blogid " + result);
63: return result;
64: } catch (Exception e) {
65: e.printStackTrace();
66: System.out.println(" UnCreated ");
67: return "failed_post";
68: }
69: }
70:
71: /*
72: * 此方法用于获取博客网站支持的日志分类,根据xml-rpc定义,获取回来的对象实际上是一个Object数组
73: * 每个数组里面包含的是一个HashMap,这个HashMap中存储的是blogcategory的信息
74: */
75: public Object[] getCatogeryObj(String username, String password)
76: throws MalformedURLException, XmlRpcException {
77: Object[] params = new Object[] { "default", username, password };
78: Object[] resultObj = (Object[]) this.blogClient.execute(
79: "metaWeblog.getCategories", params);
80: return resultObj;
81:
82: }
83:
84: /*
85: * 打印博客支持的分类信息
86: */
87: public void printBlogCategory(Object[] blogCategory) {
88: if (blogCategory != null) {
89: System.out.println("开始打印博客支持的分类");
90: for (Object blogCategoryItem : blogCategory) {
91: printBlogCategoryItem((HashMap<String, String>) blogCategoryItem);
92: System.out
93: .println("-------------------------------------------");
94: }
95: }
96: }
97:
98: /*
99: * 打印每个blogCategoryItem信息
100: * 每个blogCategoryItem由categoryid,title,htmlUrl,description,rssUrl构成
101: * 至少cnblogs的博客文章分类是如此
102: */
103: private void printBlogCategoryItem(HashMap<String, String> blogItem) {
104: if (blogItem != null & blogItem.isEmpty() == false) {
105: for (String key : blogItem.keySet()) {
106: System.out.println(key + ":" + blogItem.get(key));
107: }
108: } else {
109: System.out.println("blogItem is empty!");
110: }
111:
112: }
113:
114: /*
115: * 获取博客支持的所有博文分类名,返回的是一个String数组
116: */
117: public String[] getBlogCategoryTitle(Object[] blogCategory) {
118: if (blogCategory != null) {
119: ArrayList<String> categoryTitleList = new ArrayList<String>(20);
120: for (Object categoryItem : blogCategory) {
121: categoryTitleList.add((String) ((HashMap) categoryItem)
122: .get("title"));
123: }
124: return (String[]) categoryTitleList.toArray();
125: }
126: return null;
127: }
128: }