shokosブログ

プログラミング

英語辞書 ほぼ完成

ほぼ完成して、無事投稿できるようになりました

はてだに投稿する部分のクラス

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class HatenaContribution {
	ValueKeeper value;

	public HatenaContribution(ValueKeeper value) {
		this.value = value;
	}

	public void contribute() throws Exception {
		PrintStream ps = null;
		BufferedReader reader = null;
		try {
			String certification = WSSE.getWsseHeaderValue("syoko_sasaki",
					"ぱすわーど");
			URL url = new URL(
					"http://d.hatena.ne.jp/syoko_sasaki/atom/blog/20090617/1245222305");
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.addRequestProperty("X-WSSE", certification);
			connection.setDoOutput(true);
			OutputStream outputStream = connection.getOutputStream();
			String mainText = DiaryGetter.getMainText();
			String postXml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
					+ "<entry xmlns=\"http://purl.org/atom/ns#\">"
					+ "<title>java英語辞典</title>"
					+ "<content type=\"text/plain\">&lt;div class=\"section\""
					+ mainText + "&lt;p&gt;" + value.getenglishWord() + "   "
					+ value.getTranslationWord()
					+ "&lt;/p&gt;&lt;/div&gt;</content>" + "</entry>";
			ps = new PrintStream(outputStream, true, "UTF-8");
			ps.print(postXml);
			reader = new BufferedReader(new InputStreamReader(connection
					.getInputStream(), "utf-8"));
			String line;
			while ((line = reader.readLine()) != null) {
				System.out.println(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (ps != null) {
				ps.close();
			}
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

例外処理とかちゃんとしてないので、ちょいちょい考えて、リファクタリングできたらなーと思います。

DiaryGetterクラスについては英語辞書 - プログラミングお勉強きろくで実装したものです。


せっかく作ったのだから辞書充実させるぞ宣言