HttpClientとDigesterでRESTしてみた
Restletを使ってみたけど、オーバースペックすぎるので、結局HttpClientとDigesterで実装してみた。
対象は例によってYahoo APIのオークション。
こんな↓XMLが返るらしい。
<?xml version="1.0" encoding="UTF-8" ?> <AuctionSellingList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:jp:auc:sellinglist" xsi:schemaLocation="urn:yahoo:jp:auc:sellinglist http://auctions.yahooapis.jp/AuctionWebService/V1/SellingList.xsd" totalAuctions="3" totalPage="1" curPage="1"> <seller> <id>abc123456xyz</id> <itemlist>http://auctions.yahooapis.jp/AuctionWebService/V1/SellingList?sellerID=abc123456xyz</itemlist> <about>http://user.auctions.yahoo.co.jp/jp/show/aboutme?userID=abc123456xyz</about> <rating>http://rating5.auctions.yahoo.co.jp/jp/show/rating?userID=abc123456xyz</rating> </seller> <item> <auctionID>x9876543</auctionID> <title>【DVD】★2枚組BOX アニメーション abcdefg★</title> <url>http://page10.auctions.yahoo.co.jp/jp/auction/x9876543</url> <img>http://img213.ac.yahoo.jp.zn8.net/img213.auctions.yahoo.co.jp/users/ 4/4/1/4/abc123456xyz-thumb-1141310562 30265.jpg</img> <price>1,400 円</price> <bids>2</bids> <endtime>3月 8日 21時 43分</endtime> <option /> </item> <item> <auctionID>v32654475</auctionID> <title>【DVD】★3枚組BOX★xxxxxxxxxxxxxx★★</title> <url>http://page11.auctions.yahoo.co.jp/jp/auction/v32654475</url> <img>http://img141.ac.yahoo.jp.zn8.net/img141.auctions.yahoo.co.jp/users/ 4/4/1/4/abc123456xyz-thumb-1141310653 72374.jpg</img> <price>6,750 円</price> <bids>5</bids> <endtime>3月 8日 21時 47分</endtime> <option /> </item></AuctionSellingList>
まずは格納するBeanを作成。sellerはとりあえずいらないので、AuctionSellingListとItemを作成。
/** * Yahoo APIから返されるXMLのAuctionSellingList要素を格納するモデルクラスです。 * @author 月猫 */ public class AuctionSellingList implements Serializable { private int totalAuctions; private int totalPage; private int curPage; /** itemList */ private List<Item> itemList = new ArrayList<Item>(); /** * @param item */ public void addItem(Item item) { itemList.add(item); } /* 以下getter/setter */
/** * Yahoo APIから返されるXMLのitem要素を格納するモデルクラスです。 * @author 月猫 */ public class Item { private String auctionID; private String title; private String url; private String img; private String price; private String endtime; private String bidorbuy; /* 以下getter/setter */
HttpClientを使ってみるで作ったソースに、Digesterを使ったparse処理を追加してこんな感じに。
String url = "http://auctions.yahooapis.jp/AuctionWebService/V1/SellingList?" + "appid=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + "&sellerID=the_longbows"; HttpClient client = new HttpClient(); GetMethod method = new GetMethod(url); int result = client.executeMethod(method); if (result == HttpStatus.SC_OK) { InputStream responseStream = method.getResponseBodyAsStream(); // ルール設定 Digester d = new Digester(); // AuctionSellingListのオブジェクト作成 d.addObjectCreate("AuctionSellingList", AuctionSellingList.class); // AuctionSellingListのプロパティをセット d.addBeanPropertySetter("AuctionSellingList/totalAuctions"); d.addBeanPropertySetter("AuctionSellingList/totalPage"); d.addBeanPropertySetter("AuctionSellingList/curPage"); // Itemのオブジェクト作成 d.addObjectCreate("AuctionSellingList/item", Item.class); // AuctionSellingListにadd d.addSetNext("AuctionSellingList/item", "addItem"); // Itemのプロパティをセット d.addBeanPropertySetter("AuctionSellingList/item/auctionID"); d.addBeanPropertySetter("AuctionSellingList/item/title"); d.addBeanPropertySetter("AuctionSellingList/item/url"); d.addBeanPropertySetter("AuctionSellingList/item/img"); d.addBeanPropertySetter("AuctionSellingList/item/price"); d.addBeanPropertySetter("AuctionSellingList/item/endtime"); d.addBeanPropertySetter("AuctionSellingList/item/bidorbuy"); try { // parse実行 AuctionSellingList auctionSellingList = (AuctionSellingList) d.parse(responseStream); for (Item item : auctionSellingList.getItemList()) { // タイトルを表示してみる System.out.println(item.getTitle()); } } catch (SAXException e) { e.printStackTrace(); } }
Digesterは、あらかじめこのタグが来たらこうするみたいなルールを設定しておいて、最後にparseを実行する感じ。
簡単にできたし、今回はこれでいっか。
この辺が分かりやすいです。