Blackstar Blackstar - 1 year ago 91
JSON Question

Try to POST JSONArray with volley

I got an jsonarray like:

"color": -1,
"fill": false,
"id": 1,
"radius": 154.613,
"shapeText": "",
"shapeType": "circle",
"x1": 141.172,
"x2": 0,
"y1": 231.188,
"y2": 0
}, {
"color": -4569601,
"fill": false,
"id": 2,
"radius": 0,
"shapeText": "",
"shapeType": "rectangle",
"x1": 512.656,
"x2": 606.781,
"y1": 305.25,
"y2": 413.502

and I try to do POST to the server but I fail :[
I already get an jsonarray frm the server and I also did POST but for jsonobject to the server but I couldn't make it POST Jsonarray :[, anyone got any idea how to do it?

public class JSONPostArrayRequest extends JsonRequest<JSONObject> {
JSONArray params;
public JSONPostArrayRequest(String url, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener, JSONArray params) {
super(Method.POST, url, null, listener, errorListener);

public byte[] getBody() {
if ( this.params != null && this.params.length() > 0) {
return encodeParameters( this.params, getParamsEncoding());
return null;


private byte[] encodeParameters(JSONArray params, String paramsEncoding) {
try {
return params.toString().getBytes(paramsEncoding);
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);

protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString =
new String(, HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));


and my request done with this code:

public void updateDraw(ArrayList<Shape> shapes) {
JSONArray jsonArrayShapes = new JSONArray();
Log.d("START OF JSON ARRAY ", shapes.toString());
for (Shape shape : shapes) {
try {
JSONObject jsonObjectShape = new JSONObject();
jsonObjectShape.put("color", String.valueOf(shape.getColor()));
jsonObjectShape.put("fill", String.valueOf(shape.isFill()));
jsonObjectShape.put("radius", String.valueOf(shape.getRadius()));
jsonObjectShape.put("shapeText", String.valueOf(shape.getShapeText()));
jsonObjectShape.put("shapeType", String.valueOf(shape.getShapeType()));
jsonObjectShape.put("x1", String.valueOf(shape.getX1()));
jsonObjectShape.put("x2", String.valueOf(shape.getX2()));
jsonObjectShape.put("y1", String.valueOf(shape.getY1()));
jsonObjectShape.put("y2", String.valueOf(shape.getY2()));
jsonObjectShape.put("id", String.valueOf(shape.getId()));
} catch (JSONException e) {
Log.d("JSONARRAY = ", jsonArrayShapes.toString());
String shapeUrl = Main.GROUPS_URL + "/" + id + "/shape";
Log.d("URL = ", shapeUrl);
JSONPostArrayRequest jsonPostArrayRequest = new JSONPostArrayRequest(shapeUrl,
new Response.Listener<JSONObject>() {
public void onResponse(JSONObject response) {

new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
Log.d("onErrorResponse ", error.toString());
}, jsonArrayShapes);

Answer Source

You aren't passing your JSONArray into the request.

super(Method.POST, url, null, listener, errorListener);

That null parameter, as per the documentation

A JSONArray to post with the request. Null is allowed and indicates no parameters will be posted along with request.

Therefore, I don't see why you need to extend JsonRequest, or especially why you typed it with <JSONObject>.

The JsonArrayRequest class already exists, you just need to give the JSONArray object as the third parameter there.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download