【问题标题】:Retrieve data from Google Books API从 Google Books API 检索数据
【发布时间】:2023-03-28 17:56:01
【问题描述】:

我是 Android 新手,正在使用 Web API,我正在编写一个 Android 应用程序,它可以扫描图书的条形码,然后在 Google Books API 中搜索其 ISBN。

条形码扫描后我有这个网址:https://www.googleapis.com/books/v1/volumes?q=isbn:9788432250651&AIzaSyCpYez5556X4UzPV6rF4kkspj9DsCs_Q_c

还有下一段代码:

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

        }
    }

这不是从 API 检索任何内容。非常感谢您的帮助,谢谢!

【问题讨论】:

    标签: android google-api google-books


    【解决方案1】:

    我认为您接下来需要做的是从 API 请求连接,打开连接,使用 JSON 从 API 检索数据并使用 inputStream 获取存储在数组中的数据。 比如:在一个类中实现这些方法:

    私有静态字符串 makeHttpRequest(URL url) 抛出 IOException

    private static String readFromStream(InputStream inputStream) 抛出 IOException

    私有静态列表 extractFeatureFromJson(String booksJson)

    公共静态列表 fetchBookData(String requestUrl)

    【讨论】:

      【解决方案2】:

      Here 是一个完整的代码示例,展示了如何在 Android 中通过 Feign 或 Retrofit 使用 Google Books API。这些库在 HTTP 之上提供了更高级别的抽象,因此您可以在代码中使用简单的方法调用和对象,而不是搞乱请求、响应和 JSON 反序列化。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        • 2014-02-10
        • 2014-03-23
        • 1970-01-01
        • 2021-10-22
        • 2019-06-21
        相关资源
        最近更新 更多