尝试用齐射来发布JSONArray

时间:2016-09-15 15:19:29

标签: java android json web-services android-volley

我有一个像jsonarray:

[{
    "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
}]

我尝试对服务器进行POST但是我失败了:[ 我已经得到了一个jsonarray frm服务器,我也做了POST但是jsonobject到了服务器但我无法让它发布Jsonarray:[,有人知道怎么做吗?

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);
    this.params=params;
}

@Override
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);
    }
}

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

}

我的请求已完成此代码:

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()));
            jsonArrayShapes.put(jsonObjectShape);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        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>() {
                @Override
                public void onResponse(JSONObject response) {

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

1 个答案:

答案 0 :(得分:1)

您没有将JSONArray传递给请求。

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

null参数,根据文档

  

要发布请求的JSONArray。允许Null,表示不会随请求一起发布参数。

因此,我不明白为什么需要延长JsonRequest,或者特别是为什么要用<JSONObject>键入它。

JsonArrayRequest类已经存在,您只需要将JSONArray对象作为第三个参数。