2016-12-30 33 views
0
{ 
    "status": "true", 
    "value": { 
     "products": [ 
      { 
       "id": "1", 
       "store_categories_id": "10", 
       "description": "Chicken Briyani", 
       "unit_price": "120", 
       "pro_descripaation": "", 
       "pro_image": "" 
      }, 
      { 
       "id": "2", 
       "store_categories_id": "10", 
       "description": "Vegetable Thali", 
       "unit_price": "100", 
       "pro_descripaation": "", 
       "pro_image": "" 
      } 
     ] 
    } 
} 

錯誤:org.json.JSONException:指數2超出範圍[0..2)JSON指數2超出範圍的異常

try{ 
    JSONArray product_array = value_array.getJSONArray("products"); 
    for (int i = 0; i < response.length(); i++) { 
     if (status.equals("true")) { 
      // progressDialog.dismiss(); 

      Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show(); 
     } else if (status.equals("true")) { 

      // progressDialog.dismiss(); 
      JSONObject jsonobject = product_array.getJSONObject(i); 

      ProductList productList = new ProductList(); 

      productList.setDescription(jsonobject.getString(Constants.KEY_PRODUCT_DESCRIPTION)); 

      productList.setId(jsonobject.getString(Constants.KEY_PRODUCT_ID)); 
      Log.v("ganesh","keyboard"); 
      productListArray.add(productList); 
      adapter.notifyDataSetChanged(); 
     } 
    } 
} catch (JSONException exception) { 
    // progressDialog.dismiss(); 
    Log.e("--JSON EXCEPTION--", exception.toString()); 
} 

任何一個可以幫助我理清這個問題?

回答

1

有在你的代碼幾件事情,似乎並不正確,而是因爲我沒有在整個範圍內,特別是兩個事有蹊蹺:

  1. 您的循環是從0至response.length() ,這可能比您的product_array中的物品多。因此,您可能需要使用product_array.getJSONObject(i);或使用product_array.size()product_array.length(),depending on your import
  2. 您的if..else if不正確。您檢查status.equals("true")兩次,這意味着代碼永遠不會到達第二個if
0

私人無效callAPI_togetproducts(){enter code here // progressDialog = ProgressDialog.show(StoreMenuActivity.this, "Please wait ...", "Requesting...", true);在此輸入代碼 // progressDialog.setCancelable(假); enter code here OnRequestCompletedListener聽者=新OnRequestCompletedListener(){enter code here @覆蓋enter code here 公共無效onRequestCompleted(字符串響應){ Log.v( 「 - 輸出存儲器MENU--」,response.toString()); System.out.println(response.toString()); //progressDialog.dismiss(); 嘗試JSONObject obj = new JSONObject(response); final String status = obj.getString(「status」); final String value = obj.getString(「value」); JSONObject value_array = obj.getJSONObject(「value」);

   JSONArray product_array = value_array.getJSONArray("products"); 

       // final int product_array = response.length(); 
       //final int response = products.length(); 

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


        if (status.equals("false")) { 
         // progressDialog.dismiss(); 
         Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show(); 
        } else if (status.equals("true")) { 
         // progressDialog.dismiss(); 

         JSONObject jsonobject = product_array.getJSONObject(i); 
         ProductList productList = new ProductList(); 
         productList.setDescription(jsonobject.getString(Constants.KEY_PRODUCT_DESCRIPTION)); 
         productList.setId(jsonobject.getString(Constants.KEY_PRODUCT_ID)); 
         productListArray.add(productList); 
         adapter.notifyDataSetChanged(); 
        } 
       } 
      } catch (JSONException exception) { 
       // progressDialog.dismiss(); 
       Log.e("--JSON EXCEPTION--", exception.toString()); 
      } 
     } 
    }; 
    HashMap<String, String> user = session.getUserDetails(); 
    // token 
    String api_token = user.get(SessionManager.KEY_APITOKEN); 

    ServiceCalls.callAPI_togetproducts(this, Request.Method.POST,Constants.GET_PRODUCTS, listener, api_token,id); 
} 
+0

我認爲你有上下文 – ganii

+0

如何在上述上下文中使用product.length()? – ganii