First of all you cannot use JsonArrayRequest in this case. This is because your Myjson file does not have an array at root but rather an object.You should be using JsonObjectRequest instead.
So this is how your code should look:
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
URL,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response == null) {
Toast.makeText(getApplicationContext(), "Couldn't fetch the menu! Pleas try again.", Toast.LENGTH_LONG).show();
return;
}
Person persons = new Gson().fromJson(response.toString(), new TypeToken<Person>() {
}.getType());
// adding persons to cart list
cartList.clear();
cartList.add(persons);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("error", error.toString());
}
});
Remember to change
List<Person> persons = new Gson().fromJson(response.toString(), new TypeToken<List<Person>>() {
}.getType());
to
Person persons = new Gson().fromJson(response.toString(), new TypeToken<Person>() {
}.getType());
This is because of the same reason as above: you are getting a json object not a json array.
Now, the way Gson works is that it needs a class to 'map' the received Json to. Therefore you have to create a class. Looking at your Json, this is simple implementation of your Person class.
public class Person {
private int code;
private String message;
private List<Data> data;
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
public List<Data> getData() {
return data;
}
public class Data {
private int id;
private String name;
private String skills;
private String image;
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getSkills() {
return skills;
}
public String getImage() {
return image;
}
}}
And this is how you access the data array and its objects.
for (int i = 0; i < cartList.size(); i++) {
for (Person.Data data : cartList.get(i).getData()) {
Log.d("TAG", "Name: " + data.getName() + ", Skill: " + data.getSkills() + ", Image: " + data.getImage() + ", id: " + data.getId());
}
}
Here i am printing data in the Logcat. Make sure to do this in the onResponse() block.
And this is how the complete code looks like:
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
URL,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (response == null) {
Toast.makeText(getApplicationContext(), "Couldn't fetch the menu! Pleas try again.", Toast.LENGTH_LONG).show();
return;
}
Person persons = new Gson().fromJson(response.toString(), new TypeToken<Person>() {
}.getType());
// adding persons to cart list
cartList.clear();
cartList.add(persons);
for (int i = 0; i < cartList.size(); i++) {
for (Person.Data data : cartList.get(i).getData()) {
Log.d("TAG", "Name: " + data.getName() + ", Skill: " + data.getSkills() + ", Image: " + data.getImage() + ", id: " + data.getId());
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("error", error.toString());
}
});
requestQueue.add(request);
Hope this helps.