首先需要安装依赖环境,因为代码运行的时候会用到org.apache.http
顺便放一个net.sf.json的依赖包
整理这篇博客的目的是网上的代码有的缺一个类,有的依赖库没给,所以在学的时候费了一番功夫,特此整理。
首先是书本保存的信息类
/**
*
*/
package test;
/**
* @author ly
*
*/
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;
}
}
写一个XML的解析类
通过ISBN查询得到的信息以XML格式的形式返回,所以写一个针对此类XML格式解析的类才能得到我们想要的具体的信息(如:标题,作者,简介等等)。
/**
*
*/
package test;
/**
* @author ly
*
*/
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 tags = new ArrayList();
/**
* @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);
}
}
**最后写一个测试类 **
/**
*
*/
package test;
/**
* @author ly
*
*/
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/9787121194276");
}
}
最后,如何自动获得ISBN呢?其实用QQ,微信的扫一扫对着ISBN的二维码扫一下就可以实现,但是QQ扫出来是一串ISBN号,而微信扫出来是完整的书籍信息