Use JSONArray as request body
suggest changeThe default requests integrated in Volley do not let you pass a JSONArray as the request body of a POST request. You can pass a JSONObject as a parameter.
However, instead of passing a JSON object as a parameter to the request constructor, you need to override the getBody() method of the Request.class. You should pass null as third parameter as well:
JSONArray requestBody = new JSONArray();
new JsonObjectRequest(Request.Method.POST, REQUEST_URL, null, RESP_LISTENER, ERR_LISTENER) {
@Override
public byte[] getBody() {
try {
return requestBody.toString().getBytes(PROTOCOL_CHARSET);
} catch (UnsupportedEncodingException uee) {
// error handling
return null;
}
}
};
This constructs the request and does not add it to a RequestQueue.
Explanation of the parameters:
REQUEST_URL- The full URL to send your request to.RESP_LISTENER- AResponse.Listener<?>object, whoseonResponse(T data)method is called upon successful completion.ERR_LISTENER- AResponse.ErrorListenerobject, whoseonErrorResponse(VolleyError e)method is called upon an unsuccessful request.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents