-1

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

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

}
munna ss
  • 194
  • 1
  • 2
  • 11
  • Can you give more info or more code? Are you managing the Web Service? Your Web Server must implement some POST/PUT request to get those values and then do wherever you want. Sometimes you need authorizations to make that and give some value pairs (key and value) – Mariano Zorrilla Sep 04 '15 at 12:48
  • I think an easy way to create your JsonArray is using JSONArray. (import org.json.JSONArray) and simply send a String param in a POST using volley. – Erik Jhordan Rey Sep 04 '15 at 14:17

1 Answers1

0

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));
            }
        }
}
Kaveesh Kanwal
  • 1,753
  • 17
  • 16