【发布时间】:2018-01-22 14:52:20
【问题描述】:
我是 Android 和 Java 的新手,所以我正在构建一个图书列表演示应用程序来学习解析 JSON 数据。我使用 log 来检查数据上有多少本书(即 10 ),但在模拟器上运行它时只显示列表中的 2 本书...希望有人能告诉我应该在哪里更改? 感谢您的任何意见!
E/QueryUtils: length of array: 10
API 链接:https://www.googleapis.com/books/v1/volumes?q=search+terms
QueryUtils.java
private static List<BookItem> extractItemFromJson(String bookJSON) {
// If the JSON string is empty or null, then return early.
if (TextUtils.isEmpty(bookJSON)){
return null;
}
// Create an empty ArrayList that we can start adding books to
List<BookItem> bookItems = new ArrayList<>();
// Try to parse the JSON response string. If there is a problem with the way the JSON is formatted,
// a JSONException exception object will be thrown.
// Catch the exception so the app doesnt crash, and prent the error message to the logs.
try {
JSONObject baseJsonresponse = new JSONObject(bookJSON);
JSONArray bookItemArray = baseJsonresponse.getJSONArray("items");
Log.e("QueryUtils", "length of array: " + bookItemArray.length());
for (int i = 0; i < bookItemArray.length(); i++) {
JSONObject currentBook = bookItemArray.getJSONObject(i);
JSONObject volumeInfo = currentBook.getJSONObject("volumeInfo");
String title = volumeInfo.getString("title");
String subtitle = volumeInfo.getString("subtitle");
String previewLink = volumeInfo.getString("previewLink");
JSONObject imageLinks = volumeInfo.getJSONObject("imageLinks");
String thumbnail = imageLinks.getString("smallThumbnail");
BookItem book = new BookItem(/**author, */title, subtitle, thumbnail, previewLink);
bookItems.add(book);
}
} catch (JSONException e) {
Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
}
return bookItems;
}
【问题讨论】:
-
如果您的问题很清楚,我们可以判断。 只显示前两个在哪里?
-
@Ravi 仅在应用程序运行时在模拟器设备上的主要活动上显示前两个。嗯,我是否足够清楚?
-
是的。您需要告诉代码的一部分,即打印两个。