A_mrn_
A_mrn_

Reputation: 53

Retrieve data from Google Books API

I'm new to Android and using web APIs, and I'm writing an Android App that scans a barcode from a book and then search its ISBN in Google Books API.

I have this url after the barcode scan: https://www.googleapis.com/books/v1/volumes?q=isbn:9788432250651&AIzaSyCpYez5556X4UzPV6rF4kkspj9DsCs_Q_c

And the next code:

private class GetBookInfo extends AsyncTask <View, Void, Integer> {

        @Override
        protected Integer doInBackground(View... urls) {
            // make Call to the url
            makeCall("https://www.googleapis.com/books/v1/volumes?" +
                    "q=isbn:" + ean_content + "&AIzaSyCpYez5556X4UzPV6rF4kkspj9DsCs_Q_c");

            //print the call in the console
            System.out.println("https://www.googleapis.com/books/v1/volumes?" +
                    "q=isbn:" + ean_content + "&AIzaSyCpYez5556X4UzPV6rF4kkspj9DsCs_Q_c");

            return null;
        }


        @Override
        protected void onPreExecute() {
            // we can start a progress bar here
        }

        @Override
        protected void onPostExecute(Integer result) {

            String ruta = save_cover(getApplicationContext(), title, book_cover);

            Intent intent = new Intent(MainActivity.this, Spreadsheets.class);
//            intent.putExtra(title,title);
//            intent.putExtra(author,authors);
//            intent.putExtra(date,date);
//            intent.putExtra(category,categories);
//            intent.putExtra(description,description);
            //finish();
            startActivity(intent);
            finish();

        }
    }

    public void makeCall(String stringURL) {

        URL url = null;
        BufferedInputStream is = null;
        JsonReader jsonReader;

        try {
            url = new URL(stringURL);
        } catch (Exception ex) {
            System.out.println("Malformed URL");
        }

        try {
            if (url != null) {
                HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
                is = new BufferedInputStream(urlConnection.getInputStream());
            }
        } catch (IOException ioe) {
            System.out.println("IOException");
        }

        if (is != null) {
            try {
                jsonReader = new JsonReader(new InputStreamReader(is, "UTF-8"));
                jsonReader.beginObject();
                while (jsonReader.hasNext()) {
                    String name = jsonReader.nextName();

                    if (name.equals("title")) {
                        title = jsonReader.nextString();
                    }
                    else if (name.equals("authors")) {
                        authors = jsonReader.nextString();
                    }
                    else if (name.equals("publishedDate")) {
                       date = jsonReader.nextString();
                    }
                    else if (name.equals("categories")) {
                        categories = jsonReader.nextString();
                    }
                    else if (name.equals("description")) {
                        description = jsonReader.nextString();
                    }
//                    else if (name.equals("averageRating")) {
//                        rating = jsonReader.nextString();
//                    }
                    else if (name.equals("thumbnail")) {
                        image = jsonReader.nextString();
                        book_cover = download_cover(image);
                    }
                    else {
                        jsonReader.skipValue();
                    }
                }
                jsonReader.endObject();
            }

            catch (Exception e) {
                System.out.println("Exception");
            }

        }
    }

This isn't retrieving anything from the API. I would appreciate your help, thank you!

Upvotes: 1

Views: 2402

Answers (2)

mrts
mrts

Reputation: 18925

Here is a full code example of how to use Google Books API in Android with Feign or Retrofit. These libraries provide a higher level abstraction on top of HTTP so that you can use simple method calls and objects in your code, instead of messing with requests, responses and JSON deserialization.

Upvotes: 0

Yuba Ait Arrami
Yuba Ait Arrami

Reputation: 1

I think what you need to do next is request a connection from the API, open the connection, using JSON retrieve data from the API and use the inputStream to get the data stored in an array. something like :Implement these methods in a class:

private static String makeHttpRequest(URL url) throws IOException

private static String readFromStream(InputStream inputStream) throws IOException

private static List extractFeatureFromJson(String booksJson)

public static List featchBookData(String requestUrl)

Upvotes: 0

Related Questions