In meiner Buchlisten-App analysiere ich den JSON, um die Informationen zu extrahieren, die ich benötige, wie Autor, Herausgeber, Titel usw. Dafür habe ich verwende eine if-Anweisung, um die Zeichenfolge abzurufen, wenn die gewünschte Zeichenfolge vorhanden ist. Unten ist die Methode, wo ich das mache.Versuch, Zeichenfolge zu erhalten, wenn es Autoren, Titel, Herausgeber usw. gibt
private static List<Books> extractFeatureFromJson(String bookJSON) {
if (TextUtils.isEmpty(bookJSON)) {return null;}
List<Books> books = new ArrayList<>();
try {
JSONObject jsonResponse = new JSONObject(bookJSON);
JSONArray booksArray = jsonResponse.getJSONArray("items");
for (int i = 0; i < booksArray.length(); i++) {
JSONObject currentBook = booksArray.getJSONObject(i);
JSONObject volumeInfo = currentBook.getJSONObject("volumeInfo");
JSONArray authorsArray = volumeInfo.getJSONArray("authors");
String title, publisher, publishedDate, language, pageCount, printType, maturityRating, authors = "";
if(volumeInfo.has("title")){
language = volumeInfo.getString("language");
}
for (int j = 0; j < authorsArray.length(); j++) {
if (volumeInfo.has("authors")){
authors = authorsArray.getString(j);
}
}
if(volumeInfo.has("publisher")){
publisher = volumeInfo.getString("publisher");
}
if(volumeInfo.has("publishedDate")){
publishedDate = volumeInfo.getString("publishedDate");
}
if(volumeInfo.has("language")){
language = volumeInfo.getString("language");
}
if(volumeInfo.has("pageCount")){
pageCount = volumeInfo.getString("pageCount");
}
if(volumeInfo.has("printType")){
printType = volumeInfo.getString("printType");
}
if(volumeInfo.has("maturityRating")){
maturityRating = volumeInfo.getString("maturityRating");
}
Books book = new Books(title, authors, publisher, publishedDate, language, pageCount, printType, maturityRating);
books.add(book);
}
}
jedoch die Parameter in
Books book = new Books(title, authors, publisher, publishedDate, language, pageCount, printType, maturityRating);
books.add(book);
zeigen einen Fehler, der Variable 'x', sagt vielleicht nicht initialisiert wurden. Sowieso kann ich darüber stoßen? Ich schätze jede Hilfe.
Dies ist die JSON Google API: "https://www.googleapis.com/books/v1/volumes?maxResults=40&q="
Und hier ist das Fehlerprotokoll:
Error:(145, 40) error: variable title might not have been initialized
Error:(145, 56) error: variable publisher might not have been initialized
Error:(145, 67) error: variable publishedDate might not have been initialized
Error:(145, 82) error: variable language might not have been initialized
Error:(145, 92) error: variable pageCount might not have been initialized
Error:(145, 103) error: variable printType might not have been initialized
Error:(145, 114) error: variable maturityRating might not have been initialized
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Teilen Sie Ihre Antwort json. – Shane
Benötigen Sie weitere Informationen? Können Sie die JSON-Zeichenfolge teilen und Fehlerprotokoll – Godwin
Yep. Es ist jetzt da oben. –