{
"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)
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());
}
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:1)
你的代码中有一些东西似乎不对,但由于我没有整个上下文,特别是两件事情很奇怪:
response.length()
,这可能比product_array
中的对象多。因此,product_array.getJSONObject(i);
您将超出范围异常您可能希望使用product_array.size()
或product_array.length()
,depending on your import。if..else if
不正确。您检查两次status.equals("true")
,这意味着代码永远不会出现在第二个if
。答案 1 :(得分:0)
private void callAPI_togetproducts(){enter code here
// progressDialog = ProgressDialog.show(StoreMenuActivity.this, "Please wait ...", "Requesting...", true);
在此输入代码
// progressDialog.setCancelable(false); enter code here
OnRequestCompletedListener listener = new OnRequestCompletedListener(){enter code here
@override enter code here
public void onRequestCompleted(String response){
Log.v(“ - OUTPUT STORE 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);
}