如何修复意外的 JSON 异常 org.json.索引超出范围



为什么我会收到此错误,如何解决?我ListView有 43 件物品,index 4338从何而来?

我在这里查看了解决方案:JSONArray 异常:索引 50 超出范围 [0..50(。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;
}
}
}

在日志猫中,我多次收到错误:

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>() {

感谢您的任何帮助。

for 循环正在以下范围内迭代:(int i = 0; i < numberOfItemsInResp; i++),但numberOfItemsInRespString对象的长度。

在不深入的情况下(实际上只是基于您编写的其他代码(,我认为您最好这样做:

JSONArray responseObject = new JSONArray(response);
for (int i = 0; i < responseObject.length(); i++) {
JSONObject obj = responseObject.getJSONObject(i);
...
}

第 81 行出错的原因是您的手机Noofuser不正确,您必须对其进行修改。

final String phoneNoofUser = sharedPreferences.getString("KEY_PHONENUMBER_USER", "");

相关内容

  • 没有找到相关文章

最新更新