Issue
I am trying to put Do-While condition inside my jsonparse function (in adroid studio), but the do-while seem to be not working properly. I am also using volley. My code is shown below.
I have multiple different link that I need to parse. The idea is that if there are some jsonobject inside the link, then the code will run. Otherwise, a toast message will pop out commenting that the link does not have any jsonobject.
I have some link that does not have jsonobject, and my code work, and displaying the toast message.
I have some link where the jsonArray has "display" jsonobject, and has totalComments > 50. Then I do get the information.
The problem occurs when I have a link where jsonArray has "display" jsonobject, and also has totalComments, but all of the value is less than 50. In this case, I would like the Do-while loop to keep on checking and make sure that all of the totalComments inside the "display" jsonobject to be less than 50. Then after checked by the Do-while loop, I want to show Toast message displaying again that "there is no employee with significant comments". But instead I received the following error shown below. I do not get the toast message. I tried to pin point the problem by putting logd message around my code, but I still don't know where the problem is. I don't think my shuffleJsonArray method caused this problem though. Because I logd the shuffledJsonArray, and I can see my original jsonArray, except that they are in shuffled order.
Thank you so much for your help!
My error:
2021-06-24 11:58:18.377 3514-3514/com.example.myfirsttest W/System.err: org.json.JSONException: Index 3 out of range [0..3)
2021-06-24 11:58:18.377 3514-3514/com.example.myfirsttest W/System.err: at org.json.JSONArray.get(JSONArray.java:293)
2021-06-24 11:58:18.377 3514-3514/com.example.myfirsttest W/System.err: at org.json.JSONArray.getJSONObject(JSONArray.java:521)
2021-06-24 11:58:18.377 3514-3514/com.example.myfirsttest W/System.err: at com.example.myfirsttest.AdventureActivity$12.onResponse(AdventureActivity.java:634)
2021-06-24 11:58:18.377 3514-3514/com.example.myfirsttest W/System.err: at com.example.myfirsttest.AdventureActivity$12.onResponse(AdventureActivity.java:617)
2021-06-24 11:58:18.377 3514-3514/com.example.myfirsttest W/System.err: at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:90)
2021-06-24 11:58:18.377 3514-3514/com.example.myfirsttest W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
2021-06-24 11:58:18.377 3514-3514/com.example.myfirsttest W/System.err: at android.os.Handler.handleCallback(Handler.java:873)
2021-06-24 11:58:18.378 3514-3514/com.example.myfirsttest W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
2021-06-24 11:58:18.378 3514-3514/com.example.myfirsttest W/System.err: at android.os.Looper.loop(Looper.java:193)
2021-06-24 11:58:18.378 3514-3514/com.example.myfirsttest W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6669)
2021-06-24 11:58:18.378 3514-3514/com.example.myfirsttest W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2021-06-24 11:58:18.378 3514-3514/com.example.myfirsttest W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
2021-06-24 11:58:18.378 3514-3514/com.example.myfirsttest W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2021-06-24 11:58:18.378 3514-3514/com.example.myfirsttest W/System.err: Caused by: java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
2021-06-24 11:58:18.378 3514-3514/com.example.myfirsttest W/System.err: at java.util.ArrayList.get(ArrayList.java:437)
2021-06-24 11:58:18.378 3514-3514/com.example.myfirsttest W/System.err: at org.json.JSONArray.get(JSONArray.java:287)
2021-06-24 11:58:18.378 3514-3514/com.example.myfirsttest W/System.err: ... 12 more
My code is below:
private void JsonParse() {
String url = link; //jsonparse this link contain jsonarray of "display" with a lot of jsonobject inside this array
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("display");
arrayLength = jsonArray.length();
int totalComments = 0;
String name = "no name";
String email = "no email";
if (arrayLength > 0){
JSONArray shuffledJsonArray = shuffleJsonArray(jsonArray); //I have a method called shuffleJsonArray to shuffle the order of the jsonobject inside the "display" jsonarray.
int i = -1;
do {
i++;
JSONObject display = shuffledJsonArray.getJSONObject(i);
Log.d(TAG, "onResponse: after getting JSONobject, the result is: " + display);
if (display.has("comments")) {
totalComments = display.getInt("comments");
Log.d(TAG, "onResponse: total comments inside checking if there is comment is: " + totalComments);
} else {
totalComments = 0;
}
Log.d(TAG, "onResponse: total comments after the check is: " + totalComments);
if (totalComments > 50) {
Log.d(TAG, "onResponse: if totalComments is > 50, then this message will be run");
if (display.has("name")) {
name = display.getString("name");
}
if (display.has("email")) {
email = display.getString("email");
}
String info = "Name: " + name + "\n" +
"email: " + email + "\n" +
"total comments: " + totalComments;
}
Log.d(TAG, "onResponse: if totalComments < 50, then the if function is not run, so the totalComments result is: " + totalComments);
} while (totalComments < 50);
name = "no name";
email = "no email";
totalComments = 0;
}else {
Toast toast = Toast.makeText(AdventureActivity.this, "There is no employee with significant comments", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
public static JSONArray shuffleJsonArray(JSONArray array) throws JSONException {
// Implementing Fisher–Yates shuffle
Random rnd = new Random();
for (int i = array.length() - 1; i >= 0; i--) {
int j = rnd.nextInt(i + 1);
// Simple swap
Object object = array.get(j);
array.put(j, array.get(i));
array.put(i, object);
}
return array;
}
Solution
org.json.JSONException: Index 3 out of range [0..3)
The reason you are getting above exception is because you are trying to access an index in your array which is larger than the size of the array.
As per your code, do...while will run until the totalComments < 50 your shuffledJsonArray doesn not hold 50 items. Hence, your array is smaller in size than the condition you're trying to check. and the condition is failing.
Please put a check like
do {
i++;
if(i <= shuffledJsonArray.length()) {
// Do your operations here
} else {
// Break the loop
Toast.makeText(this, "Reached last position in array", Toast.LENGTH_SHORT).show;
break;
}
} while(totalComments < 50);
Answered By - Vikas Choudhary
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.