谷歌图书API按ISBN搜索

2022-09-01 09:32:59

我正在尝试弄清楚如何使用Google Books API通过ISBN搜索书籍。我需要编写一个程序来搜索ISBN,然后打印出标题,作者和版本。我尝试使用,但这不允许我按ISBN搜索,我没有看到获得所需信息的方法(当ISBN被放入其中时没有结果)。我现在拥有的是:List volumesList = books.volumes.list("");

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

如果有人对如何提高效率有任何建议,请告诉我。新代码:

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

答案 1

是否正在使用已弃用的数据 API

借助 Books API v1(来自 Labs),您可以使用查询

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

例如

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

按 ISBN 查询图书。

你可能想看看谷歌的示例代码:BooksSample.java


答案 2

如果我确实理解您的任务,您不能像开发人员指南开发人员指南中所说的那样尝试。你可以这样做:

BooksService booksService = new BooksService("myCompany-myApp-1");
myService.setUserCredentials("user@domain.com", "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);

现在,使用 VolumeEntry api 查找所需的 getXXXX() 并在代码中使用它。我希望它能帮助你解决你的问题。


推荐