为什么我收到此错误,我该如何解决?我的ListView
中有43个项目,index 4338
来自哪里?
我在这里查看了解决方案:JSONArray Exception : Index 50 out of range [0..50). Is there any limit on JsonArray并相应地修改了我的代码但仍然收到了错误。
这些错误出现在logcat
- 我的ListView
最终加载,大约20秒后似乎太长了 - 但只有在重复数百次后在Logcat中显示此错误之后 - 索引3829全部通往4338的路(沿途跳过一些)。
这是我的代码:
public class PopulistoListView extends AppCompatActivity {
// this is the php file name where to select from.
// we will post the user's phone number into Php and get the matching user_id
private static final String SelectUserReviews_URL = "http://www.example.com/SelectUserReviews.php";
//we are posting phoneNoofUser, the key is phonenumberofuser, which we see in php
public static final String KEY_PHONENUMBER_USER = "phonenumberofuser";
private ProgressDialog pDialog;
private List<Review> reviewList = new ArrayList<Review>();
private ListView listView;
private CustomPopulistoListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_contacts);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomPopulistoListAdapter(this, reviewList);
listView.setAdapter(adapter);
//get the phone number value from shared preferences file instead
//of from the VerifiedUserPhoneNumber class because we might not
//be coming from that class, for example on Edit, New etc. The phone
//number needs to be posted for this listview to load properly.
SharedPreferences sharedPreferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
final String phoneNoofUser = sharedPreferences.getString("phonenumberofuser", "");
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
//post the phone number of the logged in user to SelectUserReviews.php and from that
//get the logged in user's reviews
StringRequest stringRequest = new StringRequest(Request.Method.POST, SelectUserReviews_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//hide the 'loading' box when the page loads
hidePDialog();
//toast the response of SelectUserReviews.php, which has been converted to a
//JSON array in the Php file with JSON encode
Toast.makeText(PopulistoListView.this, response, Toast.LENGTH_LONG).show();
//break up the JSON Array into parts
//we need to sort out this error we keep getting in logcat
final int numberOfItemsInResp = response.length();
for (int i = 0; i < numberOfItemsInResp; i++) {
try {
JSONArray responseObject = new JSONArray(response);
JSONObject obj = responseObject.getJSONObject(i);
Review review = new Review();
review.setCategory(obj.getString("category"));
review.setName(obj.getString("name"));
review.setPhone(obj.getString("phone"));
review.setComment(obj.getString("comment"));
//we are getting the review id so we can pull extra needed info, like Address etc
review.setReviewid(obj.getString("reviewid"));
// Toast.makeText(PopulistoListView.this, responseObject.toString(), Toast.LENGTH_LONG).show();
reviewList.add(review);
} catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
// Do something to recover ... or kill the app.
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(PopulistoListView.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
//post info to php
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
//phoneNoofUser is the value we get from Android, the user's phonenumber.
//the key is "phonenumberofuser". When we see "phonenumberofuser" in our php,
//put in phoneNoofUser
params.put(KEY_PHONENUMBER_USER, phoneNoofUser);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
在logcat中我多次出错:
unexpected JSON exception
org.json.JSONException: Index 4001 out of range [0..43)
at org.json.JSONArray.get(JSONArray.java:282)
at org.json.JSONArray.getJSONObject(JSONArray.java:510)
at PopulistoListView$1.onResponse(PopulistoListView.java:96)
at PopulistoListView$1.onResponse(PopulistoListView.java:81)
第96行是:
JSONObject obj = responseObject.getJSONObject(i);
第81行是:
new Response.Listener<String>() {
感谢您的帮助。
答案 0 :(得分:6)
您的for循环正在迭代此范围:(int i = 0; i < numberOfItemsInResp; i++)
,但numberOfItemsInResp
是String
对象的长度。
没有太深入(实际上只是基于您编写的其他代码),我认为你会更好:
JSONArray responseObject = new JSONArray(response);
for (int i = 0; i < responseObject.length(); i++) {
JSONObject obj = responseObject.getJSONObject(i);
...
}
答案 1 :(得分:1)
第81行的错误原因是你的phoneNoofuser不正确,你必须修改它。
final String phoneNoofUser = sharedPreferences.getString("KEY_PHONENUMBER_USER", "");