简体   繁体   中英

How can i send a json array to webserver using volley?

I can receive a json array but how can i send a json array using volley?

 JsonArrayRequest arrayReq = new JsonArrayRequest(URL,
        new Listener<JSONArray>() {

}

Try and implement it using the snippet below, i hope it will work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM