2Stoned 2Stoned - 4 months ago 38
JSON Question

No response from jsonArray request

Well long story short i face this kind of problem, Im trying to send volley request to my server, which responds with json array:

{
"images": [{
"product_serial_num": "1",
"product_title": "Abbadon",
"product_img": "http://1.2.3.4/android/uploads/1.jpg",
"product_price": "750",
"product_description": "The destroyer"
}]
}


but no matter what i tried i still get Volley response error, my volley request:

requestQueue = Volley.newRequestQueue(getActivity());
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(MY_URL ,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//parseData to parse the json response
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

//If an error occurs that means end of the list has reached
Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
}
});

requestQueue.add(jsonArrayRequest);


the weird thing that with volley string request it does work, only the array mess things for me.

EDIT:

how ever this way i do get my array using feed:

private void getData() {
//Adding the method to the queue by calling the method getDataFromServer
requestQueue.add(getDataFromServer(requestCount));
//Incrementing the request counter
requestCount++;
}

//Request to get json from server we are passing an integer here
//This integer will used to specify the page number for the request ?page = requestcount
//This method would return a JsonArrayRequest that will be added to the request queue
private JsonArrayRequest getDataFromServer(int requestCount) {

//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL_INDEX + String.valueOf(requestCount),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Calling method parseData to parse the json response
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

//If an error occurs that means end of the list has reached
Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
}
});

//Returning the request
return jsonArrayRequest;
}


Thank you!

Answer

Use this code, Change JsonArrayRequest to JsonObjectRequest

JsonObjectRequest jsonArrayRequest = new JsonObjectRequest(Request.Method.GET, MY_URL, null,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                //parseData to parse the json response
                parseData(response);
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                //If an error occurs that means end of the list has reached
                Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
            }
        });

Array Response

"images": [{
    "product_serial_num": "1",
    "product_title": "Abbadon",
    "product_img": "http://1.2.3.4/android/uploads/1.jpg",
    "product_price": "750",
    "product_description": "The destroyer"
}]

Object Response

{
"images": [{
    "product_serial_num": "1",
    "product_title": "Abbadon",
    "product_img": "http://1.2.3.4/android/uploads/1.jpg",
    "product_price": "750",
    "product_description": "The destroyer"
}]
}
Comments