Article Directory
Preface
This article is to get through the id isbn or watercress watercress basic information about the book, watercress url interface is well known, so find out basic information call http interface via parameter passing mode.
postman calls known interfaces
We were used isbn = 9781783700608 and id = 26952493, respectively, call these two interfaces, look at the specific data, and then build their own DTO.
Examples:
isbn: https://api.douban.com/v2/book/isbn/9781783700608
doubanId: https://api.douban.com/v2/book/26952493
ISBN
postman to get watercress return the following information by passing isbn:
{
"rating": {
"max": 10,
"numRaters": 2,
"average": "0.0",
"min": 0
},
"subtitle": "",
"author": [
"Jenny Broom"
],
"pubdate": "2014-9-1",
"tags": [
{
"count": 1,
"name": "图鉴",
"title": "图鉴"
}
],
"origin_title": "",
"image": "https://img1.doubanio.com/view/subject/m/public/s29274867.jpg",
"binding": "Hardcover",
"translator": [],
"catalog": "",
"pages": "112",
"images": {
"small": "https://img1.doubanio.com/view/subject/s/public/s29274867.jpg",
"large": "https://img1.doubanio.com/view/subject/l/public/s29274867.jpg",
"medium": "https://img1.doubanio.com/view/subject/m/public/s29274867.jpg"
},
"alt": "https://book.douban.com/subject/26952493/",
"id": "26952493",
"publisher": "Big Picture Press",
"isbn10": "1783700602",
"isbn13": "9781783700608",
"title": "Animalium",
"url": "https://api.douban.com/v2/book/26952493",
"alt_title": "",
"author_intro": "",
"summary": "",
"price": "GBP 20.00"
}
Id watercress
{
"rating": {
"max": 10,
"numRaters": 2,
"average": "0.0",
"min": 0
},
"subtitle": "",
"author": [
"Jenny Broom"
],
"pubdate": "2014-9-1",
"tags": [
{
"count": 1,
"name": "图鉴",
"title": "图鉴"
}
],
"origin_title": "",
"image": "https://img1.doubanio.com/view/subject/m/public/s29274867.jpg",
"binding": "Hardcover",
"translator": [],
"catalog": "",
"pages": "112",
"images": {
"small": "https://img1.doubanio.com/view/subject/s/public/s29274867.jpg",
"large": "https://img1.doubanio.com/view/subject/l/public/s29274867.jpg",
"medium": "https://img1.doubanio.com/view/subject/m/public/s29274867.jpg"
},
"alt": "https://book.douban.com/subject/26952493/",
"id": "26952493",
"publisher": "Big Picture Press",
"isbn10": "1783700602",
"isbn13": "9781783700608",
"title": "Animalium",
"url": "https://api.douban.com/v2/book/26952493",
"alt_title": "",
"author_intro": "",
"summary": "",
"price": "GBP 20.00"
}
Can be found by calling the two interfaces, they returned entities are the same, we have to establish their own douBanInfo used to store entities according to entity type returned.
Code implements (java)
We wrote two static methods getDouBanInfoByIsbn and getDouBanInfoByDbId respectively isbn and watercress id to obtain basic information watercress.
getDouBanInfoByIsbn
public static DouBanInfo getDouBanInfoByIsbn(String isbn) {
String uri="https://api.douban.com/v2/book/isbn/"+isbn;
String douBanResult = doGet(uri, null, "UTF-8", true);
DouBanInfo douBanInfo = JSON.parseObject(douBanResult,DouBanInfo.class);
return douBanInfo;
}
getDouBanInfoByDbId
public static DouBanInfo getDouBanInfoByDbId(String douBanId) {
String uri="https://api.douban.com/v2/book/"+douBanId;
String douBanResult = doGet(uri, null, "UTF-8", true);
DouBanInfo douBanInfo = JSON.parseObject(douBanResult,DouBanInfo.class);
return douBanInfo;
}
Which doPost method:
doPost
public static String doPost(String url, Map<String, String> params, String charset, boolean pretty) {
StringBuffer response = new StringBuffer();
HttpClient client = new HttpClient();
HttpMethod method = new PostMethod(url);
//设置Http Post数据
if (params != null) {
HttpMethodParams p = new HttpMethodParams();
for (Map.Entry<String, String> entry : params.entrySet()) {
p.setParameter(entry.getKey(), entry.getValue());
}
method.setParams(p);
}
try {
client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
String line;
while ((line = reader.readLine()) != null) {
if (pretty)
response.append(line).append(System.getProperty("line.separator"));
else
response.append(line);
}
reader.close();
}
} catch (IOException e) {
log.error("执行HTTP Post请求" + url + "时,发生异常!", e);
} finally {
method.releaseConnection();
}
return response.toString();
}