Reputation: 149
I am using volley library to achive network operation when i try to ru the program using below program i am gettin warning and getting size less than what i have in the url the warning message is org.json.JSONException: Index 3 out of range [0..3).
public class MainActivity extends AppCompatActivity {
TextView results;
String JsonURL = "http://184.73.181.186/jsondata.php";
String data = "";
RequestQueue requestQueue;
MyCustomBaseAdapter myCustomBaseAdapter;
ArrayList<StudentInfo> studentInfoList = new ArrayList<StudentInfo>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestQueue = Volley.newRequestQueue(this);
results = (TextView) findViewById(R.id.jsonData);
JsonArrayRequest arrayreq = new JsonArrayRequest(JsonURL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for(int i=0;i<lenghthofarray;i++){
JSONObject jresponse = response.getJSONObject(i);
final int numberOfItemsInResp = jresponse.length();
StudentInfo studentInfo = new StudentInfo();
String fname = jresponse.getString("fName");
String lName = jresponse.getString("lName");
String rollNo = jresponse.getString("rollNo");
String profilePic = jresponse.getString("profilePic");
studentInfo.setFname(fname);
studentInfo.setLname(lName);
studentInfo.setRoolNo(rollNo);
studentInfo.setStudpic(profilePic);
studentInfoList.add(studentInfo);
list.add(profilePic);
myCustomBaseAdapter = new MyCustomBaseAdapter(getApplicationContext(),studentInfoList);
JSONObject colorObj = response.getJSONObject(0);
JSONArray colorArry = colorObj.getJSONArray("marks");
for (int ii = 0; ii < colorArry.length(); ii++) {
JSONObject jsonObject = colorArry.getJSONObject(i);
String subjectName, marks;
subjectName = jsonObject.getString("subjectName");
marks = jsonObject.getString("marks");
}
data += "\nfName " + fname +
"\nHex Value : " + lName + "\n\n\n"+rollNo+"\n"+profilePic+"\n";
}
results.setText(data);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
// Handles errors that occur due to Volley
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error");
}
}
);
}
}
Upvotes: 0
Views: 209
Reputation: 149
The problem is in(i),replace i with ii
JSONObject jsonObject = colorArry.getJSONObject(i);
solved one JSONObject jsonObject = colorArry.getJSONObject(ii);
Upvotes: 1