Google 书籍 Api - imageLinks 没有值 - Android JSONException
Google Books Api - No value for imageLinks - Android JSONException
我正在使用 Google Books Api
来显示图书列表,但是当我尝试从 imageLinks
JSONObject
获取缩略图 url 时,JSONException
表示 imageLinks
没有值,即使此对象中存在该值。
我尝试了 JSONObject.isNull()
或 optString()
之类的方法,而不是 getString()
,但它仍然没有给我任何价值。
这是我试图从中获取数据的 URL:https://www.googleapis.com/books/v1/volumes?q=android
代码如下:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener < JSONObject > () {
@Override
public void onResponse(JSONObject response) {
if (response != null) {
try {
JSONArray jsonItemsArray = response.getJSONArray("items");
for (int i = 0; i < jsonItemsArray.length(); i++) {
String thumbnailUrl = "";
String title = "";
JSONObject item = jsonItemsArray.getJSONObject(i);
JSONObject volumeInfo = item.getJSONObject("volumeInfo");
JSONObject thumbnailUrlObject = volumeInfo.getJSONObject("imageLinks");
if (!thumbnailUrlObject.isNull("thumbnail")) {
thumbnailUrl = thumbnailUrlObject.getString("thumbnail");
}
title = volumeInfo.getString("title");
bookList.add(new Book(title, thumbnailUrl));
booksAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
这是 JSON 回复的一部分:
"kind": "books#volumes",
"totalItems": 500,
"items": [
{
"kind": "books#volume",
"id": "JUVjAgAAQBAJ",
"etag": "kbnCYPNPKq4",
"selfLink": "https://www.googleapis.com/books/v1/volumes/JUVjAgAAQBAJ",
"volumeInfo": {
"title": "Android. Podstawy tworzenia aplikacji",
"authors": [
"Andrzej Stasiewicz"
],
"publisher": "Helion",
"publishedDate": "2013-11-10",
"description": "Na szczęście dostępna jest już książka Android.",
"industryIdentifiers": [
{
"type": "ISBN_13",
"identifier": "9788324688418"
},
{
"type": "ISBN_10",
"identifier": "8324688412"
}
],
"readingModes": {
"text": true,
"image": true
},
"pageCount": 216,
"printType": "BOOK",
"categories": [
"Computers"
],
"averageRating": 4.0,
"ratingsCount": 1,
"maturityRating": "NOT_MATURE",
"allowAnonLogging": true,
"contentVersion": "1.4.4.0.preview.3",
"imageLinks": {
"smallThumbnail": "http://books.google.com/books/content?id=JUVjAgAAQBAJ&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://books.google.com/books/content?id=JUVjAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
}
而且我想知道为什么 volumeInfo.getJSONObject("imageLinks")
给我没有价值的 JSONException
,即使 imageLinks
有价值。
不是每个volumeInfo
节点都有imageLinks
节点,所以你需要使用node.has
方法或node.opt*
方法检查给定节点是否存在,并检查结果是否存在null
。您可以在下方找到获取 thumbnail
节点的安全方法:
JSONObject thumbnailUrlObject = volumeInfo.optJSONObject("imageLinks");
if (thumbnailUrlObject != null && thumbnailUrlObject.has("thumbnail")) {
thumbnailUrl = thumbnailUrlObject.getString("thumbnail");
}
显示其工作原理的简单控制台应用程序:
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.nio.file.Files;
public class OrgJsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
String json = String.join("", Files.readAllLines(jsonFile.toPath()));
JSONObject response = new JSONObject(json);
// get items
JSONArray jsonItemsArray = response.getJSONArray("items");
for (int i = 0; i < jsonItemsArray.length(); i++) {
String thumbnailUrl = "";
JSONObject item = jsonItemsArray.getJSONObject(i);
JSONObject volumeInfo = item.getJSONObject("volumeInfo");
JSONObject thumbnailUrlObject = volumeInfo.optJSONObject("imageLinks");
if (thumbnailUrlObject != null && thumbnailUrlObject.has("thumbnail")) {
thumbnailUrl = thumbnailUrlObject.getString("thumbnail");
}
String title = volumeInfo.getString("title");
System.out.println("title => " + title);
System.out.println("thumbnail => " + thumbnailUrl);
}
}
}
上面的 JSON
负载打印代码:
title => Android Aplikacje wielowątkowe techniki przetwarzania
thumbnail =>
title => Android. Receptury
thumbnail => http://books.google.com/books/content?id=pZ5iAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Android. Podstawy tworzenia aplikacji
thumbnail => http://books.google.com/books/content?id=JUVjAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Optymalizacja wydajności aplikacji na Android
thumbnail => http://books.google.com/books/content?id=WJ1iAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Inteligentny dom. Automatyzacja mieszkania za pomocą platformy Arduino, systemu Android i zwykłego komputera
thumbnail => http://books.google.com/books/content?id=koiKAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Android UI. Podręcznik dla projektantów. Smashing Magazine
thumbnail => http://books.google.com/books/content?id=HEJjAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Android. Programowanie gier na tablety
thumbnail => http://books.google.com/books/content?id=7J1iAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Profesjonalne tworzenie gier internetowych dla systemu Android w językach HTML5, CSS3 i JavaScript
thumbnail => http://books.google.com/books/content?id=vlNjAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Beginning Android 2
thumbnail => http://books.google.com/books/content?id=2XeNswkT_2YC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Pro Android 2
thumbnail => http://books.google.com/books/content?id=Bam8K5SIiTkC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
我正在使用 Google Books Api
来显示图书列表,但是当我尝试从 imageLinks
JSONObject
获取缩略图 url 时,JSONException
表示 imageLinks
没有值,即使此对象中存在该值。
我尝试了 JSONObject.isNull()
或 optString()
之类的方法,而不是 getString()
,但它仍然没有给我任何价值。
这是我试图从中获取数据的 URL:https://www.googleapis.com/books/v1/volumes?q=android
代码如下:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener < JSONObject > () {
@Override
public void onResponse(JSONObject response) {
if (response != null) {
try {
JSONArray jsonItemsArray = response.getJSONArray("items");
for (int i = 0; i < jsonItemsArray.length(); i++) {
String thumbnailUrl = "";
String title = "";
JSONObject item = jsonItemsArray.getJSONObject(i);
JSONObject volumeInfo = item.getJSONObject("volumeInfo");
JSONObject thumbnailUrlObject = volumeInfo.getJSONObject("imageLinks");
if (!thumbnailUrlObject.isNull("thumbnail")) {
thumbnailUrl = thumbnailUrlObject.getString("thumbnail");
}
title = volumeInfo.getString("title");
bookList.add(new Book(title, thumbnailUrl));
booksAdapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
这是 JSON 回复的一部分:
"kind": "books#volumes",
"totalItems": 500,
"items": [
{
"kind": "books#volume",
"id": "JUVjAgAAQBAJ",
"etag": "kbnCYPNPKq4",
"selfLink": "https://www.googleapis.com/books/v1/volumes/JUVjAgAAQBAJ",
"volumeInfo": {
"title": "Android. Podstawy tworzenia aplikacji",
"authors": [
"Andrzej Stasiewicz"
],
"publisher": "Helion",
"publishedDate": "2013-11-10",
"description": "Na szczęście dostępna jest już książka Android.",
"industryIdentifiers": [
{
"type": "ISBN_13",
"identifier": "9788324688418"
},
{
"type": "ISBN_10",
"identifier": "8324688412"
}
],
"readingModes": {
"text": true,
"image": true
},
"pageCount": 216,
"printType": "BOOK",
"categories": [
"Computers"
],
"averageRating": 4.0,
"ratingsCount": 1,
"maturityRating": "NOT_MATURE",
"allowAnonLogging": true,
"contentVersion": "1.4.4.0.preview.3",
"imageLinks": {
"smallThumbnail": "http://books.google.com/books/content?id=JUVjAgAAQBAJ&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://books.google.com/books/content?id=JUVjAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
}
而且我想知道为什么 volumeInfo.getJSONObject("imageLinks")
给我没有价值的 JSONException
,即使 imageLinks
有价值。
不是每个volumeInfo
节点都有imageLinks
节点,所以你需要使用node.has
方法或node.opt*
方法检查给定节点是否存在,并检查结果是否存在null
。您可以在下方找到获取 thumbnail
节点的安全方法:
JSONObject thumbnailUrlObject = volumeInfo.optJSONObject("imageLinks");
if (thumbnailUrlObject != null && thumbnailUrlObject.has("thumbnail")) {
thumbnailUrl = thumbnailUrlObject.getString("thumbnail");
}
显示其工作原理的简单控制台应用程序:
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.nio.file.Files;
public class OrgJsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
String json = String.join("", Files.readAllLines(jsonFile.toPath()));
JSONObject response = new JSONObject(json);
// get items
JSONArray jsonItemsArray = response.getJSONArray("items");
for (int i = 0; i < jsonItemsArray.length(); i++) {
String thumbnailUrl = "";
JSONObject item = jsonItemsArray.getJSONObject(i);
JSONObject volumeInfo = item.getJSONObject("volumeInfo");
JSONObject thumbnailUrlObject = volumeInfo.optJSONObject("imageLinks");
if (thumbnailUrlObject != null && thumbnailUrlObject.has("thumbnail")) {
thumbnailUrl = thumbnailUrlObject.getString("thumbnail");
}
String title = volumeInfo.getString("title");
System.out.println("title => " + title);
System.out.println("thumbnail => " + thumbnailUrl);
}
}
}
上面的 JSON
负载打印代码:
title => Android Aplikacje wielowątkowe techniki przetwarzania
thumbnail =>
title => Android. Receptury
thumbnail => http://books.google.com/books/content?id=pZ5iAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Android. Podstawy tworzenia aplikacji
thumbnail => http://books.google.com/books/content?id=JUVjAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Optymalizacja wydajności aplikacji na Android
thumbnail => http://books.google.com/books/content?id=WJ1iAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Inteligentny dom. Automatyzacja mieszkania za pomocą platformy Arduino, systemu Android i zwykłego komputera
thumbnail => http://books.google.com/books/content?id=koiKAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Android UI. Podręcznik dla projektantów. Smashing Magazine
thumbnail => http://books.google.com/books/content?id=HEJjAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Android. Programowanie gier na tablety
thumbnail => http://books.google.com/books/content?id=7J1iAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Profesjonalne tworzenie gier internetowych dla systemu Android w językach HTML5, CSS3 i JavaScript
thumbnail => http://books.google.com/books/content?id=vlNjAgAAQBAJ&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Beginning Android 2
thumbnail => http://books.google.com/books/content?id=2XeNswkT_2YC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
title => Pro Android 2
thumbnail => http://books.google.com/books/content?id=Bam8K5SIiTkC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api