Volley - Sending a POST request using JSONArrayRequest

Marc Mailhot picture Marc Mailhot · Aug 5, 2013 · Viewed 56.8k times · Source

I'm using Volley to interact with an API. I need to send a post request (with parameters) to a service that returns a JSON Array.

JsonObjectRequest has a constructor that takes a method and a set of parameters

JsonObjectRequest(int method, java.lang.String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) 

However JSONArrayRequest (the one I need) only has one constructor of the form

JsonArrayRequest(java.lang.String url, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) 

How can I make this send a POST request with data?

Answer

Itai Hanski picture Itai Hanski · Aug 5, 2013

They're probably going to add it later, but in the meanwhile you can add the wanted constructor yourself:

public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
        Listener<JSONArray> listener, ErrorListener errorListener) {
    super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), 
        listener, errorListener);
}

This isn't tested, though I see no reason this shouldn't work since the implementation details are in the super class: JsonRequest.

Try it and see if it works.

EDIT:

I called it! It took them almost two years after I answered this but the Volley team added this constructor on March 19, 2015 to the repo. Guess what? This is the EXACT syntax.