Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley - Sending a POST request using JSONArrayRequest

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?

like image 215
Marc Mailhot Avatar asked Aug 04 '13 23:08

Marc Mailhot


1 Answers

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.

like image 53
Itai Hanski Avatar answered Sep 24 '22 16:09

Itai Hanski