今期放送されているアニメ情報の取得
現在作成中のアニメ情報収集サイトでは、今期放送されているアニメを毎期入力している。
最初は手作業で追加していたが、面倒になってきたので、ある程度自動化した。
自動化する際に
アニメ番組表 API | アニメマップ
のAPIを利用させていただいた。
APIの提供ありがとうございます。
http://animemap.net/api/table/tokyo.json
にアクセスすると東京で放映されるアニメ情報がJSON形式で返ってくるので、これをそのままGWTに渡し、表の形にして表示している。
サーバー側のサービスにgetAnimemapJsonというメソッドを追加し、
@Override public String getAnimemapJson() { try { URL url = new URL("http://animemap.net/api/table/tokyo.json"); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); return IOUtils.toString(reader); } catch (IOException e) { log.severe(e.getMessage()); } return null; }
GWTのJSONParserを使って、JSONをパースする。
public class AnimemapJson { private JSONObject json; public AnimemapJson(String json) { this.json = JSONParser.parseStrict(json).isObject(); } public List<String> getTitleList() { JSONObject responseObject = json.get("response").isObject(); JSONArray itemArray = responseObject.get("item").isArray(); List<String> titleList = new ArrayList<String>(); for (int i = 0; i < itemArray.size(); i++) { JSONObject itemObject = itemArray.get(i).isObject(); JSONString titleString = itemObject.get("title").isString(); titleList.add(titleString.stringValue()); } return titleList; } }