In my project I am trying ti fetch data, the data is inside an Array so I did the steps needed but the Array has a name in the data which I dont know how to pass "listCar"[]
.
Here is my code of fetching:
private void getData() {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject jsonObject = response.getJSONObject(i);
CarClass car = new CarClass();
car.setTitle(jsonObject.getString("adTitle"));
car.setDate(jsonObject.getString("adDate"));
car.setPrice(jsonObject.getDouble("adPrice"));
car.setCategory(jsonObject.getString("category"));
car.setBrand(jsonObject.getString("brand"));
car.setBrandModel(jsonObject.getString("brandModel"));
car.setKilometer(jsonObject.getDouble("kilometer"));
car.setModelYear(jsonObject.getString("modelYear"));
car.setFuel(jsonObject.getString("fuelType"));
car.setGear(jsonObject.getString("gearType"));
carClassList.add(car);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
here is the Log error I get:
E/Volley: com.android.volley.ParseError: org.json.JSONException: Value of type org.json.JSONObject cannot be converted to JSONArray
Response:
{
"listCar": [
{
"id": 204,
"adTitle": null,
"adDate": "2019-11-07T11:52:40.0156875",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 203,
"adTitle": null,
"adDate": "2019-11-07T11:52:37.7771547",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 202,
"adTitle": null,
"adDate": "2019-11-07T11:52:35.5569602",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 201,
"adTitle": null,
"adDate": "2019-11-07T11:52:33.2120309",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 200,
"adTitle": null,
"adDate": "2019-11-07T11:52:30.8982098",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 199,
"adTitle": null,
"adDate": "2019-11-07T11:52:15.1157196",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 198,
"adTitle": null,
"adDate": "2019-11-07T11:50:09.5922072",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 197,
"adTitle": null,
"adDate": "2019-11-07T11:44:08.8795626",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 196,
"adTitle": null,
"adDate": "2019-11-07T11:42:40.3744716",
"adPrice": 25,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 6,
"brandModel": "M300",
"kilometer": 300,
"modelYearId": 2,
"modelYear": "2010",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "automatic",
"carFeatureGearId": 2,
"image": null,
"vendorId": 1
},
{
"id": 195,
"adTitle": null,
"adDate": "2019-11-07T09:34:43.119591",
"adPrice": 2400,
"category": "car child 1",
"categoryId": 7,
"status": 1,
"brandId": 1,
"brand": "mercedes",
"brandModelId": 5,
"brandModel": "M500",
"kilometer": 170000,
"modelYearId": 4,
"modelYear": "2012",
"fuelType": "benz",
"carFeatureFuelId": 3,
"gearType": "normal",
"carFeatureGearId": 1,
"image": {
"path": "Upload/UploadCarMain/UploadCarMain-200-200/30032235992008220498istanbul_adalarCar195.jpg",
"name": "30032235992008220498istanbul_adalarCar195.jpg"
},
"vendorId": 14
}
],
"count": 74,
"responseStatusCode": 1,
"descriptionResponse": "Success"
}
How can I pass the "listCar" to my response in order to access the data?
You have to use JsonObjectRequest
instead of JsonArrayRequest
as your response is actually a JSONObject
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("listCar");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
CarClass car = new CarClass();
car.setTitle(jsonObject.getString("adTitle"));
car.setDate(jsonObject.getString("adDate"));
// Left of the implementation
}
} catch (JSONException ex) {
ex.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjectRequest);