2016-07-02 24 views
1

我試圖解析以下JSON數據到本書的作者和標題存儲到ListView控件:無法加載JSON解析數據到ArrayList和顯示在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中的錯誤。

+0

你能否粘貼你的適配器代碼? –

回答

1

首先,JSON字符串沒有被正確關閉,它應該是:

{"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"]}}]} 

現在,您的分析功能做它的工作正常,你只要裏面的嵌套了一個小錯誤,它應該是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顯示的東西,所以我想你有另一個bug在適配器中的代碼或在您的書類。

+0

我對使用計數器j而不是i進行了小改動。 但儘管如此,我無法填充ListView。 – sshashankk

+0

你能告訴我你的適配器代碼嗎?請 –