2

I want to POST JsonObject from device to server using Volley but I couldn't find any code example. If you can please provide me with some references or some code example.

Jilberta
  • 2,836
  • 5
  • 30
  • 44

2 Answers2

4

I do it this way

public void doRequest(RequestQueue volleyRequestQueue,
        onResponse responseListener) {

    this._responseListener = responseListener;

    StringRequest stringRequest = new StringRequest(Method.POST,
            Settings.QUESTIONURL, this, this) {

        public String getBodyContentType() {
            return "application/json; charset=" + getParamsEncoding();
        }

        public byte[] getBody() throws AuthFailureError {
            try {
                return new GsonBuilder()
                        .excludeFieldsWithoutExposeAnnotation().create()
                        .toJson(YOUROBJECT).toString()
                        .getBytes(getParamsEncoding());
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

    };

    stringRequest.setRetryPolicy(new DefaultRetryPolicy(10000, MAXRETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    volleyRequestQueue.add(stringRequest);
}
A.S.
  • 4,574
  • 3
  • 26
  • 43
1

I found a good reference, where are some good examples :

Link

Jilberta
  • 2,836
  • 5
  • 30
  • 44