2014-01-13 23 views
-2

我使用Volley JSON請求從Web服務中解析JSON。Android - 解析了不完整的JSON響應

這是它的樣子:

{ 
    "results": [ 
     { 
      "category": "Wallet", 
      "thumbnail": { 
       "image50": "http://ked.ai/uploads/item/1718946498/60284_1389592558.4e549bae0384ffce63035a7689d824c4_50.jpg", 
       "image100": "http://ked.ai/uploads/item/1718946498/60284_1389592558.4e549bae0384ffce63035a7689d824c4_100.jpg" 
      }, 
      "price": "166", 
      "shop_vanity": "EProductsSibu", 
      "description": "DESCRIPTION: Good Leather. Made in India.\r\n\r\nPACKAGE CONTENT: Good Condition\r\n\r\nREASON FOR SALE: One more extra\r\n\r\nCOD LOCATION: Sibu, Sarawak", 
      "item_id": "18319", 
      "name": "Always Wild Men's Wallet With Chain Ring", 
      "created_at": "2014-01-13 13:56:01", 
      "category_friendly": "wallet", 
      "user_id": "60284" 
     }, 
     { 
      "category": "Accessories", 
      "thumbnail": { 
       "image50": "http://ked.ai/uploads/item/656783513/1802_1389589673.a4bc91735db236661b207534e72849bb_50.jpg", 
       "image100": "http://ked.ai/uploads/item/656783513/1802_1389589673.a4bc91735db236661b207534e72849bb_100.jpg" 
      }, 
      "price": "10", 
      "shop_vanity": "mhs", 
      "description": "BROOCH CINCIN SESUAI DIPAKAI OLEH MEREKA YG GEMAR BERTUDUNG BAWAL. BOLEH MENJIMATKAN MASA SEKALIGUS TAMPAK ANGGUN ;)\r\n\r\n", 
      "item_id": "18307", 
      "name": "Brooch Cincin", 
      "created_at": "2014-01-13 13:10:19", 
      "category_friendly": "accessories", 
      "user_id": "1802" 
     }, 
     { 
      "category": "Accessories", 
      "thumbnail": { 
       "image50": "http://ked.ai/uploads/item/656783513/1802_1389589678.8c60ab80a3eea04dc74605d3e4df2d8b_50.jpg", 
       "image100": "http://ked.ai/uploads/item/656783513/1802_1389589678.8c60ab80a3eea04dc74605d3e4df2d8b_100.jpg" 
      }, 
      "price": "10", 
      "shop_vanity": "mhs", 
      "description": "BROOCH CINCIN SESUAI DIPAKAI OLEH MEREKA YG GEMAR BERTUDUNG BAWAL. BOLEH MENJIMATKAN MASA SEKALIGUS TAMPAK ANGGUN ;)", 
      "item_id": "18308", 
      "name": "Brooch Cincin", 
      "created_at": "2014-01-13 13:10:19", 
      "category_friendly": "accessories", 
      "user_id": "1802" 
     }, 
     { 
      "category": "Accessories", 
      "thumbnail": { 
       "image50": "http://ked.ai/uploads/item/656783513/1802_1389589683.1b19cc2dedfde693784e8cd915b45227_50.jpg", 
       "image100": "http://ked.ai/uploads/item/656783513/1802_1389589683.1b19cc2dedfde693784e8cd915b45227_100.jpg" 
      }, 
      "price": "10", 
      "shop_vanity": "mhs", 
      "description": "BROOCH CINCIN SESUAI DIPAKAI OLEH MEREKA YG GEMAR BERTUDUNG BAWAL. BOLEH MENJIMATKAN MASA SEKALIGUS TAMPAK ANGGUN ;)", 
      "item_id": "18309", 
      "name": "Brooch Cincin", 
      "created_at": "2014-01-13 13:10:19", 
      "category_friendly": "accessories", 
      "user_id": "1802" 
     }, 
     { 
      "category": "Accessories", 
      "thumbnail": { 
       "image50": "http://ked.ai/uploads/item/656783513/1802_1389589687.93389c64d7e36d7ebfaf58dbbb77914a_50.jpg", 
       "image100": "http://ked.ai/uploads/item/656783513/1802_1389589687.93389c64d7e36d7ebfaf58dbbb77914a_100.jpg" 
      }, 
      "price": "10", 
      "shop_vanity": "mhs", 
      "description": "BROOCH CINCIN SESUAI DIPAKAI OLEH MEREKA YG GEMAR BERTUDUNG BAWAL. BOLEH MENJIMATKAN MASA SEKALIGUS TAMPAK ANGGUN ;)", 
      "item_id": "18310", 
      "name": "Brooch Cincin", 
      "created_at": "2014-01-13 13:10:19", 
      "category_friendly": "accessories", 
      "user_id": "1802" 
     }, 
     { 
      "category": "Accessories", 
      "thumbnail": { 
       "image50": "http://ked.ai/uploads/item/656783513/1802_1389589692.21d560bc5aec0e594bfc1598b9c8f3ca_50.jpg", 
       "image100": "http://ked.ai/uploads/item/656783513/1802_1389589692.21d560bc5aec0e594bfc1598b9c8f3ca_100.jpg" 
      }, 
      "price": "10", 
      "shop_vanity": "mhs", 
      "description": "BROOCH CINCIN SESUAI DIPAKAI OLEH MEREKA YG GEMAR BERTUDUNG BAWAL. BOLEH MENJIMATKAN MASA SEKALIGUS TAMPAK ANGGUN ;)", 
      "item_id": "18311", 
      "name": "Brooch Cincin", 
      "created_at": "2014-01-13 13:10:19", 
      "category_friendly": "accessories", 
      "user_id": "1802" 
     }, 
     { 
      "category": "Accessories", 
      "thumbnail": { 
       "image50": "http://ked.ai/uploads/item/656783513/1802_1389589697.01e964c85afb4a2b1e8caf2bcf5ad169_50.jpg", 
       "image100": "http://ked.ai/uploads/item/656783513/1802_1389589697.01e964c85afb4a2b1e8caf2bcf5ad169_100.jpg" 
      }, 
      "price": "10", 
      "shop_vanity": "mhs", 
      "description": "BROOCH CINCIN SESUAI DIPAKAI OLEH MEREKA YG GEMAR BERTUDUNG BAWAL. BOLEH MENJIMATKAN MASA SEKALIGUS TAMPAK ANGGUN ;)", 
      "item_id": "18312", 
      "name": "Brooch Cincin", 
      "created_at": "2014-01-13 13:10:19", 
      "category_friendly": "accessories", 
      "user_id": "1802" 
     } 
    ] 
} 

但是,我得到了一個不完整的JSON響應。只有前兩個結果對象被成功解析。這裏是解析json的方法:

private void loadItemList() { 
    mRequestQueue = Volley.newRequestQueue(getActivity()); 
    pd = ProgressDialog.show(getActivity(), null, "Loading..."); 

    String url = Constants.LATEST_ITEM_LIST; 

    JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET, url, 
      null, new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        Log.i(TAG, response.toString()); 
        parseJSON(response); 
        va.notifyDataSetChanged(); 
        pd.dismiss(); 
       } 
      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        pd.dismiss(); 
        Log.i(TAG, error.getMessage()); 

        errorMsg = VolleyErrorHelper.getMessage(error, 
          getActivity()); 

        Toast.makeText(getActivity(), errorMsg, 
          Toast.LENGTH_LONG).show(); 
       } 
      }); 

    mRequestQueue.add(jr); 
} 

private void parseJSON(JSONObject json) { 
    if (json != null) { 
     try { 
      JSONArray items = json.getJSONArray("results"); 
      for (int i = 0; i < json.length(); i++) { 
       JSONObject itemlist = items.getJSONObject(i); 

       ItemListModel ilm = new ItemListModel(); 
       ilm.setCategory(itemlist.optString(Constants.TAG_CATEGORY_NAME)); 
       ilm.setItem_id(itemlist.optString(Constants.TAG_ITEM_ID)); 
       ilm.setName(itemlist.optString(Constants.TAG_ITEM_NAME)); 
       ilm.setPrice(itemlist.optString(Constants.TAG_ITEM_PRICE)); 
       ilm.setUser_id(itemlist.optString(Constants.TAG_USER_ID)); 

       JSONObject thumb = itemlist.getJSONObject(Constants.TAG_ITEM_THUMBNAIL); 
       ilm.setThumbnail(thumb.optString(Constants.TAG_ITEM_THUMB)); 

       arrItemList.add(ilm); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } else { 
     Log.d("Item list: ", "null"); 
    } 
} 

上面的邏輯有什麼問題嗎?

+0

它總是更好地使用GSON或JACKSON lib中JSON解析,您使用的是你爲什麼要使用JSON解析手動舊方法有效的lib凌空?創建POJO並開始使用GSON庫! –

回答

1

在這裏,你已經解析您的JSON數組一樣

JSONArray items = json.getJSONArray("results"); 

因此,在你的循環,而不是使用JSON對象,你必須使用你的項目 JsonArray。因此,您可以訪問其中包含的其他數據。

因此改變從

for (int i = 0; i < json.length(); i++) 

for (int i = 0; i < items .length(); i++) 
+0

哈哈,小錯。謝謝 – AimanB

+0

@AimanB很樂意幫助你! – Piyush

0

因爲的JSONObjectjson.length();你獲得長度試試這個..

你的循環,你需要從items.length();JSONArray得到長度不JSONObject的

JSONArray items = json.getJSONArray("results"); 
for (int i = 0; i < items.length(); i++) { 
0

在你的For循環你已經定義了你的數組大小json對象不是你的JsonArray可能這就是爲什麼它沒有給出prope r結果。

將您的items JsonArray的大小放在您的for循環中,如items.length()

試試如下:

JSONArray items = json.getJSONArray("results"); 
     for (int i = 0; i < items.length(); i++) {