Volley JsonObjectRequest Post request not working Volley JsonObjectRequest Post request not working android android

Volley JsonObjectRequest Post request not working


try to use this helper class

import java.io.UnsupportedEncodingException;import java.util.Map;    import org.json.JSONException;import org.json.JSONObject;    import com.android.volley.NetworkResponse;import com.android.volley.ParseError;import com.android.volley.Request;import com.android.volley.Response;import com.android.volley.Response.ErrorListener;import com.android.volley.Response.Listener;import com.android.volley.toolbox.HttpHeaderParser;public class CustomRequest extends Request<JSONObject> {    private Listener<JSONObject> listener;    private Map<String, String> params;    public CustomRequest(String url, Map<String, String> params,            Listener<JSONObject> reponseListener, ErrorListener errorListener) {        super(Method.GET, url, errorListener);        this.listener = reponseListener;        this.params = params;    }    public CustomRequest(int method, String url, Map<String, String> params,            Listener<JSONObject> reponseListener, ErrorListener errorListener) {        super(method, url, errorListener);        this.listener = reponseListener;        this.params = params;    }    protected Map<String, String> getParams()            throws com.android.volley.AuthFailureError {        return params;    };    @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));        }    }    @Override    protected void deliverResponse(JSONObject response) {        // TODO Auto-generated method stub        listener.onResponse(response);    }}

In activity/fragment do use this

RequestQueue requestQueue = Volley.newRequestQueue(getActivity());CustomRequest jsObjRequest = new CustomRequest(Method.POST, url, params, this.createRequestSuccessListener(), this.createRequestErrorListener());requestQueue.add(jsObjRequest);


You can create a custom JSONObjectReuqest and override the getParams method, or you can provide them in the constructor as a JSONObject to be put in the body of the request.

Like this (I edited your code):

JSONObject obj = new JSONObject();obj.put("id", "1");obj.put("name", "myname");RequestQueue queue = MyVolley.getRequestQueue();JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,SPHERE_URL,obj,    new Response.Listener<JSONObject>() {        @Override        public void onResponse(JSONObject response) {             System.out.println(response);             hideProgressDialog();        }    },    new Response.ErrorListener() {        @Override        public void onErrorResponse(VolleyError error) {             hideProgressDialog();        }    });queue.add(jsObjRequest);


Easy one for me ! I got it few weeks ago :

This goes in getBody() method, not in getParams() for a post request.

Here is mine :

    @Override/** * Returns the raw POST or PUT body to be sent. * * @throws AuthFailureError in the event of auth failure */public byte[] getBody() throws AuthFailureError {    //        Map<String, String> params = getParams();    Map<String, String> params = new HashMap<String, String>();    params.put("id","1");    params.put("name", "myname");    if (params != null && params.size() > 0) {        return encodeParameters(params, getParamsEncoding());    }    return null;}

(I assumed you want to POST the params you wrote in your getParams)

I gave the params to the request inside the constructor, but since you are creating the request on the fly, you can hard coded them inside your override of the getBody() method.

This is what my code looks like :

    Bundle param = new Bundle();    param.putString(HttpUtils.HTTP_CALL_TAG_KEY, tag);    param.putString(HttpUtils.HTTP_CALL_PATH_KEY, url);    param.putString(HttpUtils.HTTP_CALL_PARAM_KEY, params);    switch (type) {    case RequestType.POST:        param.putInt(HttpUtils.HTTP_CALL_TYPE_KEY, RequestType.POST);        SCMainActivity.mRequestQueue.add(new SCRequestPOST(Method.POST, url, this, tag, receiver, params));

and if you want even more this last string params comes from :

param = JsonUtils.XWWWUrlEncoder.encode(new JSONObject(paramasJObj)).toString();

and the paramasJObj is something like this : {"id"="1","name"="myname"} the usual JSON string.