Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Volley, JsonObjectRequest but receiving JsonArray

So I am using a JsonObjectRequest to send up a JsonObject to a rest call, but its returning a JsonArray rather then a JsonObject. Its giving me an error saying that it cannot parse the results from the JsonObjectRequest, but if I use JsonArrayRequest i cant send up a JsonObject in the body. How do I send up a JsonObject but get a JsonArray as a response?

        RequestQueue queue = Volley.newRequestQueue(this);
    JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,jsonBody,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    String test = "";
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
like image 949
Pandamonium99 Avatar asked Feb 02 '16 06:02

Pandamonium99


1 Answers

I faced this situation recently and realized that Volley doesn't offer any out of the box solution for this. You have create a custom response that takes in a json object request and returns an array. Once you create your own class you will be able to do something like this.

 CustomJsonRequest jsonObjectRequest = new CustomJsonRequest(Request.Method.POST, url, credentials, new Response.Listener<JSONArray>(){...}



package com.example.macintosh.klickcard.Helpers.Network;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * Created by yasinyaqoobi on 10/6/16.
 */

public class CustomJsonRequest<T> extends JsonRequest<JSONArray> {

    private JSONObject mRequestObject;
    private Response.Listener<JSONArray> mResponseListener;

    public CustomJsonRequest(int method, String url, JSONObject requestObject, Response.Listener<JSONArray> responseListener,  Response.ErrorListener errorListener) {
        super(method, url, (requestObject == null) ? null : requestObject.toString(), responseListener, errorListener);
        mRequestObject = requestObject;
        mResponseListener = responseListener;
    }

    @Override
    protected void deliverResponse(JSONArray response) {
        mResponseListener.onResponse(response);
    }

    @Override
    protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
            try {
                String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
                try {
                    return Response.success(new JSONArray(json),
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (JSONException e) {
                    return Response.error(new ParseError(e));
                }
            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            }
    }
}
like image 60
Yasin Yaqoobi Avatar answered Sep 28 '22 02:09

Yasin Yaqoobi