gpt4 book ai didi

android - 无法将 JSON 解析的数据加载到 ArrayList 中并显示在 ListView 上

转载 作者:行者123 更新时间:2023-11-30 01:13:49 25 4
gpt4 key购买 nike

我正在尝试解析以下 JSON 数据以将书的作者和标题存储到 ListView 中:

{
"kind": "books#volumes",
"totalItems": 921,
"items": [{
"kind": "books#volume",
"id": "QP7VvnhDOOsC",
"etag": "BQzzyT9zza0",
"selfLink": "https://www.googleapis.com/books/v1/volumes/QP7VvnhDOOsC",
"volumeInfo": {
"title": "Programming Android",
"authors": ["Zigurd Mednieks", "Laird Dornin", "G. Blake Meike", "Masumi Nakamura"],
"publisher": "\"O'Reilly Media, Inc.\"",
"publishedDate": "2012",
"description": "Presents instructions for creating Android applications for mobile devices using Java.",
"industryIdentifiers": [{
"type": "ISBN_13",
"identifier": "9781449316648"
}, {
"type": "ISBN_10",
"identifier": "1449316646"
}],
"readingModes": {
"text": false,
"image": true
},
"pageCount": 542,
"printType": "BOOK",
"categories": ["Computers"],
"averageRating": 3.5,
"ratingsCount": 6,
"maturityRating": "NOT_MATURE",
"allowAnonLogging": false,
"contentVersion": "preview-1.0.0",
"imageLinks": {
"smallThumbnail": "http://books.google.co.in/books/content?id=QP7VvnhDOOsC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://books.google.co.in/books/content?id=QP7VvnhDOOsC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
},
"language": "en",
"previewLink": "http://books.google.co.in/books?id=QP7VvnhDOOsC&printsec=frontcover&dq=android+intitle&hl=&cd=1&source=gbs_api",
"infoLink": "http://books.google.co.in/books?id=QP7VvnhDOOsC&dq=android+intitle&hl=&source=gbs_api",
"canonicalVolumeLink": "http://books.google.co.in/books/about/Programming_Android.html?hl=&id=QP7VvnhDOOsC"
},
"saleInfo": {
"country": "IN",
"saleability": "NOT_FOR_SALE",
"isEbook": false
},
"accessInfo": {
"country": "IN",
"viewability": "PARTIAL",
"embeddable": true,
"publicDomain": false,
"textToSpeechPermission": "ALLOWED",
"epub": {
"isAvailable": false
},
"pdf": {
"isAvailable": false
},
"webReaderLink": "http://books.google.co.in/books/reader?id=QP7VvnhDOOsC&hl=&printsec=frontcover&output=reader&source=gbs_api",
"accessViewStatus": "SAMPLE",
"quoteSharingAllowed": false
},
"searchInfo": {
"textSnippet": "Presents instructions for creating Android applications for mobile devices using Java."
}
}, {
"kind": "books#volume",
"id": "S1QQ-mOaPd8C",
"etag": "hbStdCg087k",
"selfLink": "https://www.googleapis.com/books/v1/volumes/S1QQ-mOaPd8C",
"volumeInfo": {
"title": "Sams Teach Yourself Java in 21 Days (Covering Java 7 and Android)",
"authors": ["Rogers Cadenhead"],

以下是我用于 JSON 解析并将数据添加到 ArrayList 的方法。

public void DisplayList(String result) {
try {
JSONObject jsonobject = new JSONObject(result);
JSONArray jarray = jsonobject.getJSONArray("items");

for (int i = 0; i < jarray.length(); i++) {
JSONObject volumeInfo = jarray.getJSONObject(i).getJSONObject("volumeInfo");
String book_title = volumeInfo.getString("title");
JSONArray authors = volumeInfo.getJSONArray("authors");
for (int j = 0; j < authors.length(); j++) {
String book_author = authors.getString(i);
books.add(new Book(book_title, book_author));
}
}
} catch (JSONException e) {
e.printStackTrace();
}

BooksAdapter adapter = new BooksAdapter(this, books);
ListView listView = (ListView) findViewById(R.id.list);

listView.setAdapter(adapter);

//listView.setVisibility((adapter.isEmpty())?View.GONE:View.VISIBLE);
if (adapter.isEmpty()) {
defaultTextView.setText(no_response_message[2]);
//"No data to display, please enter correct keyword and search again."

}
}

当我使用函数 books.add() 时,它运行完美,但是当我尝试显示 ListView 时,我弹出 ListView 空消息。

可能是什么错误导致我无法将数据存储到 ArrayList 中。

最佳答案

首先JSON String没有正常关闭,应该是:

{"kind":"books#volumes","totalItems":921,"items":[{"kind":"books#volume","id":"QP7VvnhDOOsC","etag":"BQzzyT9zza0","selfLink":"https://www.googleapis.com/books/v1/volumes/QP7VvnhDOOsC","volumeInfo":{"title":"Programming Android","authors":["Zigurd Mednieks","Laird Dornin","G. Blake Meike","Masumi Nakamura"],"publisher":"\"O'Reilly Media, Inc.\"","publishedDate":"2012","description":"Presents instructions for creating Android applications for mobile devices using Java.","industryIdentifiers":[{"type":"ISBN_13","identifier":"9781449316648"},{"type":"ISBN_10","identifier":"1449316646"}],"readingModes":{"text":false,"image":true},"pageCount":542,"printType":"BOOK","categories":["Computers"],"averageRating":3.5,"ratingsCount":6,"maturityRating":"NOT_MATURE","allowAnonLogging":false,"contentVersion":"preview-1.0.0","imageLinks":{"smallThumbnail":"http://books.google.co.in/books/content?id=QP7VvnhDOOsC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api","thumbnail":"http://books.google.co.in/books/content?id=QP7VvnhDOOsC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"},"language":"en","previewLink":"http://books.google.co.in/books?id=QP7VvnhDOOsC&printsec=frontcover&dq=android+intitle&hl=&cd=1&source=gbs_api","infoLink":"http://books.google.co.in/books?id=QP7VvnhDOOsC&dq=android+intitle&hl=&source=gbs_api","canonicalVolumeLink":"http://books.google.co.in/books/about/Programming_Android.html?hl=&id=QP7VvnhDOOsC"},"saleInfo":{"country":"IN","saleability":"NOT_FOR_SALE","isEbook":false},"accessInfo":{"country":"IN","viewability":"PARTIAL","embeddable":true,"publicDomain":false,"textToSpeechPermission":"ALLOWED","epub":{"isAvailable":false},"pdf":{"isAvailable":false},"webReaderLink":"http://books.google.co.in/books/reader?id=QP7VvnhDOOsC&hl=&printsec=frontcover&output=reader&source=gbs_api","accessViewStatus":"SAMPLE","quoteSharingAllowed":false},"searchInfo":{"textSnippet":"Presents instructions for creating Android applications for mobile devices using Java."}},{"kind":"books#volume","id":"S1QQ-mOaPd8C","etag":"hbStdCg087k","selfLink":"https://www.googleapis.com/books/v1/volumes/S1QQ-mOaPd8C","volumeInfo":{"title":"Sams Teach Yourself Java in 21 Days (Covering Java 7 and Android)","authors":["Rogers Cadenhead"]}}]}

现在,你的解析函数正确地完成了它的工作,你只是在你的嵌套 for 中有一个小错误,它应该是 authors.getString(j) 而不是 authors.getString(i )。 for 循环最终会是:

for (int i = 0; i < jarray.length(); i++) {
JSONObject volumeInfo = jarray.getJSONObject(i).getJSONObject("volumeInfo");
String book_title = volumeInfo.getString("title");
JSONArray authors = volumeInfo.getJSONArray("authors");
for (int j = 0; j < authors.length(); j++) {
String book_author = authors.getString(j);
books.add(new Book(book_title, book_author));
}
}

无论如何,您应该仍会在 ListView 中显示一些内容,所以我猜您的适配器代码或 Book 类中还有另一个错误。

关于android - 无法将 JSON 解析的数据加载到 ArrayList 中并显示在 ListView 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38162554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com