Search code examples
javaisbn

How does one access the ISBNDB using Java to find info on a book? Is it possible to use the ISBNDB api without using Maven or Gradle?


I'm trying to access the ISBNDB and get information about all the relavent data about a book so I can make a bibliography from that data for a school project. But, I'm having trouble accessing the ISBNDB and using the ISBNDB API through Java. I have never used Maven or Gradle before, so is it possible to use the ISBNDB API without using Maven or Gradle? And if so, can I get some example code to see how to use the ISBNDB API in Java?

I've tried using the example code from the ISBNDB API and plugging that in to my Java project, but it didn't compile or work correctly. It doesn't seem like the colon is supposed to be in the setRequestProperty method because when I tried putting the exact code into my compiler (IntelliJ), it wouldn't compile and it would just be angry with me. I've made a Rest Key on the ISBNDB website and put that into my code, which it was "YOUR_REST_KEY". I did not change it from the example. I tried changing the colon to a comma, but that didn't help compile the code.

private static HttpURLConnection con;

public static void main(String[] args) throws MalformedURLException,
        ProtocolException, IOException {

    String url = "https://api2.isbndb.com/book/9781934759486";

    try {

        URL myurl = new URL(url);
        con = (HttpURLConnection) myurl.openConnection();
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("Authorization": "YOUR_REST_KEY");
        con.setRequestMethod("GET");

        StringBuilder content;

        try (BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()))) {

            String line;
            content = new StringBuilder();

            while ((line = in.readLine()) != null) {
                content.append(line);
                content.append(System.lineSeparator());
            }
        }

        System.out.println(content.toString());

    } finally {

        con.disconnect();
    }
}

I expected that the content that was meant to be printed was all the info about the book from the ISBN (like author, title, publisher, publish date, etc.), but only a compile error came up and nothing was outputted to the console.


Solution

  • I figured out what was wrong and that was the fact that in order to use this API, you have to pay for it and I did not realize this when I tried using it. I thought it was free, but turns out it was not so I didn't have the authority to use the API.