Java-JSONArray有10个对象,但只显示前两个

Java-JSONArray有10个对象,但只显示前两个,java,android,json,parsing,Java,Android,Json,Parsing,我不熟悉Android和Java,所以我正在构建一个图书列表的演示应用程序,以了解如何解析JSON数据。我使用日志检查数据上有多少本书(10本),但在模拟器上运行时,只显示列表上的前两本书。。。希望有人能告诉我在哪里换车? 谢谢你的意见 E/QueryUtils: length of array: 10 API链接: QueryUtils.java private static List<BookItem> extractItemFromJson(String bookJSON

我不熟悉Android和Java,所以我正在构建一个图书列表的演示应用程序,以了解如何解析JSON数据。我使用日志检查数据上有多少本书(10本),但在模拟器上运行时,只显示列表上的前两本书。。。希望有人能告诉我在哪里换车? 谢谢你的意见

E/QueryUtils: length of array: 10

API链接:

QueryUtils.java

 private static List<BookItem> extractItemFromJson(String bookJSON) {
        // If the JSON string is empty or null, then return early.
        if (TextUtils.isEmpty(bookJSON)){
            return null;
        }

    // Create an empty ArrayList that we can start adding books to
    List<BookItem> bookItems = new ArrayList<>();

    // Try to parse the JSON response string. If there is a problem with the way the JSON is formatted,
    // a JSONException exception object will be thrown.
    // Catch the exception so the app doesnt crash, and prent the error message to the logs.
    try {
        JSONObject baseJsonresponse = new JSONObject(bookJSON);
        JSONArray bookItemArray = baseJsonresponse.getJSONArray("items");
        Log.e("QueryUtils", "length of array: " + bookItemArray.length());
        for (int i = 0; i < bookItemArray.length(); i++) {
            JSONObject currentBook = bookItemArray.getJSONObject(i);
            JSONObject volumeInfo = currentBook.getJSONObject("volumeInfo");
            String title = volumeInfo.getString("title");
            String subtitle = volumeInfo.getString("subtitle");
            String previewLink = volumeInfo.getString("previewLink");
            JSONObject imageLinks = volumeInfo.getJSONObject("imageLinks");
            String thumbnail = imageLinks.getString("smallThumbnail");

            BookItem book = new BookItem(/**author, */title, subtitle, thumbnail, previewLink);
            bookItems.add(book);
        }
    } catch (JSONException e) {
        Log.e("QueryUtils", "Problem parsing the earthquake JSON results", e);
    }

    return bookItems;
}
private静态列表extractItemFromJson(字符串bookJSON){
//如果JSON字符串为空或null,则提前返回。
if(TextUtils.isEmpty(bookJSON)){
返回null;
}
//创建一个空的ArrayList,我们可以开始向其中添加书籍
List bookItems=new ArrayList();
//尝试解析JSON响应字符串。如果JSON的格式有问题,
//将抛出JSONException异常对象。
//捕获异常,使应用程序不会崩溃,并将错误消息发送到日志中。
试一试{
JSONObject baseJsonresponse=新的JSONObject(bookJSON);
JSONArray bookItemArray=baseJsonresponse.getJSONArray(“items”);
Log.e(“QueryUtils”,“数组长度:”+bookItemArray.length());
对于(int i=0;i
我怀疑它在解析前2个后会进入catch块,因为解析时发生异常,您应该修复异常,还可以在循环中捕获异常以添加可以解析的项,并且如果存在
JSONException
,列表中的第三本书不包含字段
imageLinks
,则不要退出循环。
您应该添加一张支票:

String thumbnail = "";
if (volumeInfo.has("imageLinks")) {
    JSONObject imageLinks = volumeInfo.getJSONObject("imageLinks");
    thumbnail = imageLinks.getString("smallThumbnail");
}

如果你的问题清楚的话,我们可以告诉你。仅显示前两个where?@Ravi仅在应用程序运行时在模拟器设备上的主活动上显示前两个。。嗯,我说得够清楚了吗?是的。您需要告诉代码的一部分,即打印两个。因为在分析任何特定原因时发生异常?@Ravi不,因为我想不出其他原因,我知道问题应该更清楚。@AhmedHegazy oh yass,谢谢。日志说imageLinks没有值。理解最初的问题,并尝试现在就弄清楚为什么imageLinks现在没有价值。。。(虽然我还不知道它为什么这么说)是的!非常感谢你。。我忘了第三本书会在位置(2)而不是(3)。。