如果有作者,标题,出版商等,试图获取字符串

时间:2016-11-03 07:05:15

标签: java android json

在我的书籍列表应用程序中,我正在解析JSON以提取我需要的信息,例如作者,出版商,标题等。为此,我使用if语句来获取所需字符串的字符串存在。以下是我正在这样做的方法。

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);
            }


        }

然而,

中的参数
Books book = new Books(title, authors, publisher, publishedDate, language, pageCount, printType, maturityRating);
                books.add(book);

显示错误,表示变量“x”可能尚未初始化。无论如何,我可以碰到这个?我感谢所有的帮助。

这是JSON Google API:"https://www.googleapis.com/books/v1/volumes?maxResults=40&q="

这是错误日志:

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.

1 个答案:

答案 0 :(得分:1)

您在代码中声明了一些字符串。

String title, publisher, publishedDate, language, pageCount, printType, maturityRating, authors = "";

但是,因为错误消息告诉您只有最后一个被初始化。 您必须初始化所有变量。像这样:

String title = "", publisher = "", publishedDate = "", language = "", pageCount = "", printType = "", maturityRating = "", authors = "";