@[toc]

序言

本文主要是通过isbn或者豆瓣的id来获取豆瓣书籍的基本信息,豆瓣接口的url是已知的,通过传参的方式调用http接口从而查出基本信息。

postman调用已知接口

我们分别用isbn=9781783700608和id=26952493,分别调用这两个接口,来看看具体的数据,然后建自己的DTO。 例子:     isbn: https://api.douban.com/v2/book/isbn/9781783700608     doubanId: https://api.douban.com/v2/book/26952493

ISBN

postman通过传入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

{
  "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"
}

可以通过调用两个接口可以发现,它们所返回的实体都是一样的,我们根据所返回的实体类型建立自己的douBanInfo用来存放实体。

代码实现(java)

我们写了两个静态方法getDouBanInfoByIsbn和getDouBanInfoByDbId分别用isbn和豆瓣id来获取豆瓣的基本信息。 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;
}

其中doPost方法为: 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();
        }