2011-10-26 47 views
21

Tôi đang cố gắng tìm cách tìm kiếm sách theo ISBN bằng API Google Sách. Tôi cần viết chương trình tìm kiếm ISBN rồi in ra tiêu đề, tác giả và ấn bản. Tôi đã thử sử dụng List volumesList = books.volumes.list(""); nhưng điều đó không cho phép tôi tìm kiếm theo ISBN và tôi không thấy cách nào để lấy thông tin mình cần (khi ISBN được đặt trong đó không có kết quả). Những gì tôi có ngay bây giờ là:Tìm kiếm API sách của Google theo ISBN

JsonFactory jsonFactory = new JacksonFactory();  
    final Books books = new Books(new NetHttpTransport(), jsonFactory); 
    List volumesList = books.volumes.list("9780262140874"); 

    volumesList.setMaxResults((long) 2); 

    volumesList.setFilter("ebooks"); 
    try 
    { 
     Volumes volumes = volumesList.execute(); 
     for (Volume volume : volumes.getItems()) 
     { 
      VolumeVolumeInfo volumeInfomation = volume.getVolumeInfo(); 
      System.out.println("Title: " + volumeInfomation.getTitle()); 
      System.out.println("Id: " + volume.getId()); 
      System.out.println("Authors: " + volumeInfomation.getAuthors()); 
      System.out.println("date published: " + volumeInfomation.getPublishedDate()); 
      System.out.println(); 
     } 

    } catch (Exception ex) { 
     // TODO Auto-generated catch block 
     System.out.println("didnt wrork "+ex.toString()); 
    } 

Nếu có bất kỳ đề xuất nào về cách làm cho điều này hiệu quả hơn, hãy cho tôi biết. Mã mới:

String titleBook=""; 

    //////////////////////////////////////////////// 
    try 
    {        
     BooksService booksService = new BooksService("UAH"); 
     String isbn = "9780262140874"; 
     URL url = new URL("http://www.google.com/books/feeds/volumes/?q=ISBN%3C" + isbn + "%3E"); 
     VolumeQuery volumeQuery = new VolumeQuery(url); 
     VolumeFeed volumeFeed = booksService.query(volumeQuery, VolumeFeed.class); 
     VolumeEntry bookInfo=volumeFeed.getEntries().get(0); 

     System.out.println("Title: " + bookInfo.getTitles().get(0)); 
     System.out.println("Id: " + bookInfo.getId()); 
     System.out.println("Authors: " + bookInfo.getAuthors()); 
     System.out.println("Version: " + bookInfo.getVersionId()); 
     System.out.println("Description: "+bookInfo.getDescriptions()+"\n"); 
     titleBook= bookInfo.getTitles().get(0).toString(); 
     titleBook=(String) titleBook.subSequence(titleBook.indexOf("="), titleBook.length()-1); 
    }catch(Exception ex){System.out.println(ex.getMessage());} 
    ///////////////////////////////////////////////// 
    JsonFactory jsonFactory = new JacksonFactory();  
    final Books books = new Books(new NetHttpTransport(), jsonFactory); 
    List volumesList = books.volumes.list(titleBook); 
    try 
    { 
     Volumes volumes = volumesList.execute(); 
     Volume bookInfomation= volumes.getItems().get(0); 

     VolumeVolumeInfo volumeInfomation = bookInfomation.getVolumeInfo(); 
     System.out.println("Title: " + volumeInfomation.getTitle()); 
     System.out.println("Id: " + bookInfomation.getId()); 
     System.out.println("Authors: " + volumeInfomation.getAuthors()); 
     System.out.println("date published: " + volumeInfomation.getPublishedDate()); 
     System.out.println(); 

    } catch (Exception ex) { 
     System.out.println("didnt wrork "+ex.toString()); 
    } 

Trả lời

39

Bạn đang sử dụng deprecated data API?

Với Books API v1 (từ Labs) bạn có thể sử dụng truy vấn

https://www.googleapis.com/books/v1/volumes?q=isbn:<your_isbn_here> 

ví dụ

https://www.googleapis.com/books/v1/volumes?q=isbn:0735619670

để truy vấn một cuốn sách của ISBN của nó.

Bạn có thể muốn xem mã ví dụ của Googles: BooksSample.java

+0

Cách bạn đề xuất là dành cho HTML chứ không phải java. Cách BooksSample thực hiện nó giống như tôi đã nói ở trên và không cho phép tìm kiếm theo ISBN.List volumesList = books.volumes.list (truy vấn) là mã họ sử dụng cũng là truy vấn là một chuỗi. –

+9

Truy vấn sử dụng yêu cầu HTTP GET và nhận phản hồi ở định dạng JSON, không độc lập về ngôn ngữ và thường được sử dụng trong Java. Không có HTML liên quan. – Chris

+1

xin lưu ý: https://www.googleapis.com/books/v1/volumes?q=isbn0735619670 trả lại "totalItems": 78 colon fwd! không thể tin rằng đó không phải là tài liệu ở đâu đó (atleast tôi đã không tìm thấy nó) https://www.googleapis.com/books/v1/volumes?q=isbn <0735619670> trả về "totalItems": 511 nhưng https: //www.googleapis.com/books/v1/volumes?q=isbn:0735619670 thực hiện tìm kiếm chính xác và trả về "totalItems": 1. FWD Đại tá! Không thể tin rằng đó không phải là tài liệu ở đâu đó (atleast tôi đã không tìm thấy nó) – masi

4

bạn không thể thử như thế này như đã nói ở các nhà phát triển hướng dẫn developer guide nếu tôi đã hiểu nhiệm vụ của bạn. Bạn có thể làm như thế này:

BooksService booksService = new BooksService("myCompany-myApp-1"); 
myService.setUserCredentials("[email protected]", "secretPassword"); 

String isbn = "9780552152679"; 
URL url = new URL("http://www.google.com/books/feeds/volumes/?q=ISBN%3C" + isbn + "%3E"); 
VolumeQuery volumeQuery = new VolumeQuery(url); 
VolumeFeed volumeFeed = booksService.query(volumeQuery, VolumeFeed.class); 

// using an ISBN in query gives only one entry in VolumeFeed 
List<VolumeEntry> volumeEntries = volumeFeed.getEntries(); 
VolumeEntry entry = volumeEntries.get(0); 

Bây giờ sử dụng giao diện VolumeEntry api cho getXXXX mong muốn của bạn() và sử dụng nó trong code.I bạn hy vọng nó sẽ giúp bạn giải quyết vấn đề của bạn.

+1

Câu trả lời của bạn hoạt động khá tốt Nó đã cho tôi tiêu đề chính xác của sách có ISBN tương ứng. Tuy nhiên, ngay cả khi tôi gọi bookInfo.getAuthors() nó đã trả về một danh sách trống. Khi in nó trông giống như sau "Tác giả: []" –

+1

có thể làm bookInfo.getCreators()? Nó sẽ trả về danh sách người sáng tạo như là trường hợp của Người sáng tạo. Sau đó, bạn có thể nhận được các tác giả. Có thể nó sẽ hoạt động như tôi đã không thử nó. Thử. – Shahriar

+0

Tôi đã có thể làm cho nó hoạt động với mã mới ở trên. Cảm ơn bạn đã giúp đỡ. –

Các vấn đề liên quan