豆瓣API获取图书信息
在本篇文章中,主要是通过豆瓣API实现获取图书信息的小功能。
一. 豆瓣API能干什么?
参考链接:[url]http://www.douban.com/service/ [/url]
豆瓣API是豆瓣为第三方开发人员提供的编程接口。利用豆瓣API,你可以在你的网站或程序中使用豆瓣的数据和功能.目前的豆瓣API支持的功能包括:
搜索并查看书籍、电影、音乐信息 搜索并查看用户信息,查看用户友邻信息 查看用户收藏 添加、更新、删除用户收藏 查看评论 发布、修改、删除评论 查看、添加、删除用户广播 查看、添加、删除用户日记 搜索并查看、添加、删除活动 查看、添加、删除、回复推荐 ... ...
二. 根据书本ISBN来获取图书信息
豆瓣API提供了根据ISBN来查询书本信息的服务,链接:
http://api.douban.com/book/subject/isbn/+ISBN。如ISBN为9787308083256,我们就可以通过链接(http://api.douban.com/book/subject/isbn/9787308083256)获取到下面的信息。
在这个返回的XML文件中,包含了书籍的诸多信息,如书本的题目,作者,内容摘要,出版日期…等等。
三. 解析XML来获取书本的详细信息
3.1写一个豆瓣的书本类
import java.io.Serializable;public class TudouBookInfo implements Serializable {private static final long serialVersionUID = 2179631010054135058L;private String tags;//书本标签private String isbn10;//10位ISBNprivate String isbn13;private String title;private String pages;private String author;private String price;private String binding;private String publisher;private String pubdate;private String summary;private String imagePath;/** * @return the imagePath */public String getImagePath() {return imagePath;}/** * @param imagePath * the imagePath to set */public void setImagePath(String imagePath) {this.imagePath = imagePath;}public TudouBookInfo() {}/** * @return the tags */public String getTags() {return tags;}/** * @param tags * the tags to set */public void setTags(String tags) {this.tags = tags;}/** * @return the isbn10 */public String getIsbn10() {return isbn10;}/** * @param isbn10 * the isbn10 to set */public void setIsbn10(String isbn10) {this.isbn10 = isbn10;}/** * @return the isbn13 */public String getIsbn13() {return isbn13;}/** * @param isbn13 * the isbn13 to set */public void setIsbn13(String isbn13) {this.isbn13 = isbn13;}/** * @return the title */public String getTitle() {return title;}/** * @param title * the title to set */public void setTitle(String title) {this.title = title;}/** * @return the pages */public String getPages() {return pages;}/** * @param pages * the pages to set */public void setPages(String pages) {this.pages = pages;}/** * @return the author */public String getAuthor() {return author;}/** * @param author * the author to set */public void setAuthor(String author) {this.author = author;}/** * @return the price */public String getPrice() {return price;}/** * @param price * the price to set */public void setPrice(String price) {this.price = price;}/** * @return the binding */public String getBinding() {return binding;}/** * @param binding * the binding to set */public void setBinding(String binding) {this.binding = binding;}/** * @return the publisher */public String getPublisher() {return publisher;}/** * @param publisher * the publisher to set */public void setPublisher(String publisher) {this.publisher = publisher;}/** * @return the pubdate */public String getPubdate() {return pubdate;}/** * @param pubdate * the pubdate to set */public void setPubdate(String pubdate) {this.pubdate = pubdate;}/** * @return the summary */public String getSummary() {return summary;}/** * @param summary * the summary to set */public void setSummary(String summary) {this.summary = summary;}}
import java.io.InputStream;import java.util.ArrayList;import java.util.List;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.DefaultHandler;public class BookXMLParser extends DefaultHandler {private TudouBookInfo book = null;private final StringBuilder buff = new StringBuilder();private String attname = null;private final List<String> tags = new ArrayList<String>();/** * @return the book */public TudouBookInfo getBook() {return book;}public BookXMLParser(InputStream is) {try {SAXParserFactory spfactory = SAXParserFactory.newInstance();spfactory.setValidating(false);SAXParser saxParser = spfactory.newSAXParser();XMLReader xmlReader = saxParser.getXMLReader();xmlReader.setContentHandler(this);xmlReader.parse(new InputSource(is));} catch (Exception e) {System.err.println(e);System.exit(1);}}public void startElement(String uri, String localName, String name,Attributes atts) throws SAXException {if (name.equalsIgnoreCase("entry")) {book = new TudouBookInfo();} else if (name.equalsIgnoreCase("db:attribute")) {attname = atts.getValue("name");} else if (name.equalsIgnoreCase("db:tag")) {tags.add(atts.getValue("name"));} else if (name.equalsIgnoreCase("link")) {if ("image".equalsIgnoreCase(atts.getValue("rel"))) {book.setImagePath(atts.getValue("href"));}}buff.setLength(0);}public void endElement(String uri, String localName, String name)throws SAXException {if ("entry".equalsIgnoreCase(name)) {StringBuilder str = new StringBuilder();for (String t : tags) {str.append(t + "/");}book.setTags(str.toString());} else if (name.equalsIgnoreCase("db:attribute")) {String value = buff.toString().trim();if ("isbn10".equalsIgnoreCase(attname)) {book.setIsbn10(value);} else if ("isbn13".equalsIgnoreCase(attname)) {book.setIsbn13(value);} else if ("title".equalsIgnoreCase(attname)) {book.setTitle(value);} else if ("pages".equalsIgnoreCase(attname)) {book.setPages(value);} else if ("author".equalsIgnoreCase(attname)) {book.setAuthor(value);} else if ("price".equalsIgnoreCase(attname)) {book.setPrice(value);} else if ("publisher".equalsIgnoreCase(attname)) {book.setPublisher(value);} else if ("binding".equalsIgnoreCase(attname)) {book.setBinding(value);} else if ("pubdate".equalsIgnoreCase(attname)) {book.setPubdate(value);}} else if ("summary".equalsIgnoreCase(name)) {book.setSummary(buff.toString());}buff.setLength(0);}public void characters(char ch[], int start, int length)throws SAXException {buff.append(ch, start, length);}}
import java.io.IOException;import java.io.InputStream;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;public class RetrieveDocumentByURL {public RetrieveDocumentByURL(String url) throws ClientProtocolException, IOException{DefaultHttpClient client = new DefaultHttpClient();HttpGet get = new HttpGet(url);HttpResponse response = client.execute(get); HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); TudouBookInfo book = new BookXMLParser(is).getBook(); System.out.println("title:->" + book.getTitle()); System.out.println("summary:->"+ book.getSummary()); System.out.println("price:-->" + book.getPrice()); System.out.println("author:-->" + book.getAuthor()); System.out.println("ImagePath:-->" + book.getImagePath());}public static void main(String[] args) throws ClientProtocolException, IOException {new RetrieveDocumentByURL("http://api.douban.com/book/subject/isbn/9787308083256");}}