通过豆瓣Api,输入ISBN获取图书信息

来源:互联网 发布:追一个女生有多难 知乎 编辑:程序博客网 时间:2024/04/30 19:46
在本篇文章中,主要是通过豆瓣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位ISBN    private 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;  }}

3.2写一个XML的解析类 
通过ISBN查询得到的信息以XML格式的形式返回,所以写一个针对此类XML格式解析的类才能得到我们想要的具体的信息(如:标题,作者,简介等等)。 

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);  }}

3.3最后写一个测试类 
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");  }}

Eclipse工具中设置断点,查看得到的书本具体信息如下图所示: 

控制台输出 : 
title:->马云的颠覆智慧 
summary:->他是“教主”!是极具煽动力的“布道者”!是不走寻常路的企业家! 
在阿里巴巴、淘宝一个个大放异彩的案例中,他如何以颠覆完成超越? 
他自称完全不懂网络,却打造了阿里巴巴帝国,颠覆了中国互联网生态;他演讲激情澎湃,不走寻常路,成为一名孜孜不倦的布道者;他宣称在阿里巴巴,股东的地位在顾客、员工之后,股东却纷纷向他伸出橄榄枝!他如何以颠覆完成超越? 
本书正是从分析马云异于常人的企业运营智慧和理念出发,用清晰地案例和深刻的分析,展现其以颠覆完成超越的独到智慧。 
关于“颠覆”,马云如是说—— 
做任何事,必须要有突破,没有突破,就等于没做。 
——马云在《赢在中国》节目中的点评 
世界永远不缺创新,永远不缺的是借口。 
——2010年马云在IT领袖峰会上的演讲 
今时今日,一场由互联网技术掀起的革命正初露端倪,这股浪潮必将永久改变顾客与企业之间的力量态势。在世界各地,能够把握这些新契机和新趋势的中小型企业必将在竞争中脱颖而出。 
——2009年马云在新加坡APEC中小企业高峰会议的演讲《因小而美》 
假如没有变革,怎么会有中小企业,假如没有变革,我们这些所有垄断的企业,怎么有利益在?所以说不破不立。 
——2008年马云新浪博客文章《呼唤企业家精神 坚持梦想敢于担当》 

蓝海战略是一种颠覆性的思考。 
——2006年马云在浙商大会暨首届浙商投资博览会上的发言 
阿里巴巴进入淘宝,将会颠覆C2C、B2C等概念,而未来两到三年内,阿里巴巴与淘宝也必然走向融合,这是一个大趋势。 
——2005年12月25日上海交通大学安泰管理学院演讲 
price:-->35.00元 
author:-->快刀洪七 
ImagePath:-->http://img3.douban.com/spic/s4644461.jpg 


至此,通过豆瓣提供的API,一个简单的图书信息获取小程序就完成了。 

四.获取图书信息后能做什么呢? 
获取书本信息之后,最简单的应用就可以为自己建立一个图书信息库,实现一个简单的Web应用。比如将得到的书籍信息存入到数据库中,然后以分页的形式将书本信息显示在页面上,最后用户可以点击某个书本,弹出一个框显示详细信息,如下图: 


0 0