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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With