【发布时间】:2020-07-07 19:11:48
【问题描述】:
我无法在此 Json 响应中提取图书缩略图的网址: https://www.googleapis.com/books/v1/volumes?q=java&maxResults=10
当我将链接硬编码到 String 值时,它可以工作,所以它一定是我获取缩略图的方法。
这是我的代码:
try {
// Create a JSONObject from the JSON response string
JSONObject baseJsonResponse = new JSONObject(bookwormJSON);
// Extract the JSONArray associated with the key called "items",
// which represents a list of Books.
JSONArray bookwormArray = baseJsonResponse.getJSONArray("items");
// For each bookworm in the bookwormArray, create an {@link Book} object
for (int i = 0; i < bookwormArray.length(); i++) {
JSONObject volumeInfo = bookwormArray.getJSONObject(i).getJSONObject("volumeInfo");
String author = "dork";
// Extract the value for the key called "title"
String title = volumeInfo.getString("title");
// Extract the value for the key called "author - I just get first one in array"
JSONArray authors = volumeInfo.getJSONArray("authors");
author = authors.getString(0);
String imgUrl = volumeInfo.getString("smallThumbnail");
imgUrl = imgUrl.substring(0, 4) + 's' + imgUrl.substring(4);
// title, author and url from the JSON response.
Book bookworm = new Book(title, author,imgUrl );
// When url hardcoded and above related code to url is removed it works.
// Add the new {@link Earthquake} to the list of Books.
bookworms.add(bookworm);
}
}
【问题讨论】: