我在尝试从 google 本书 api 中检索数据时遇到错误

I'm getting an error trying to retrieve data from google books api

我刚开始在 android 中使用 API,所以我正在使用 Google 书籍 API 制作一个简单的应用程序,所以我想检索标题、作者和描述。这是提取信息的方法,但它抛出异常并执行 catch 块。这是查询“https://www.googleapis.com/books/v1/volumes?q=android&maxResults=1”,其中 bookJSON 变量是查询响应:

 private static List<Book> extractFeatureFromJson(String bookJSON) {

        if (TextUtils.isEmpty(bookJSON)) {
            return null;
        }

                    List<Book> books = new ArrayList<>();


        try {

            // Create a JSONObject from the JSON response string
            JSONObject baseJsonResponse = new JSONObject(bookJSON);


            JSONArray itemsArray = baseJsonResponse.getJSONArray("items");


            for (int i = 0; i < itemsArray.length(); i++) {

                           JSONObject currentBook = itemsArray.getJSONObject(i);

                JSONArray authorArray = currentBook.getJSONArray("authors");

                StringBuilder authorsStringBuild = new StringBuilder();

                for(int j=0; j<authorArray.length(); j++){
                    authorsStringBuild.append(authorArray.getString(j));
                    authorsStringBuild.append(", ");
                }

                String authors = authorsStringBuild.toString();

                String title = currentBook.getString("title");

                String description = currentBook.getString("description");

                double rating = currentBook.getDouble("averageRating");

                Book book = new Book( title, authors ,description , rating );

                books.add(book);
            }

        } catch (JSONException e) {

            Log.e(LOG_TAG, "Problem parsing the book JSON results", e);
        }

        return books;
    }

以后请post输出和错误日志。我没有足够的声誉来评论这一点。 我 运行 你的代码,这是一个 JSON 解析错误。 在您的代码中替换此行

JSONArray authorArray = currentBook.getJSONArray("authors");

有了这个

itemsArray.getJSONObject(i).getJSONObject("volumeInfo").getJSONArray("authors");

您在 "authors" 之前缺少 JSON 对象。

您可以使用在线 JSON 格式化工具更好地可视化层次结构,让您的生活更轻松。这是我喜欢的https://jsonformatter.curiousconcept.com/