How to send json object to server using volley in android

I want to send a JSONObject to the server using the POST method. I used the volleyball library to pass string parameters with my working effect, but if I try to use the json object, its error display for calling the json object here is my code

private void makeJsonObjReq() { showProgressDialog(); JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, Const.URL_LOGIN, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.d(TAG, response.toString()); msgResponse.setText(response.toString()); hideProgressDialog(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.d(TAG, "Error: " + error.getMessage()); hideProgressDialog(); } }) { /** * Passing some request headers * */ @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json; charset=utf-8"); return headers; } @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("un", "xyz@gmail.com"); params.put("p", "somepasswordhere"); return params; } }; // Adding request to request queue AppController.getInstance().addToRequestQueue(jsonObjReq,tag_json_obj); // Cancelling request // ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj); } 

And my error form server: [10031] BasicNetwork.performRequest: Unexpected response code 401

How to solve this problem. I want to add application/json;charset=utf-8 in the header, please check my code to see if it is correct or not. Please give me a suggestion to solve this problem.

+8
json android android-volley
source share
1 answer

The third parameter in JsonObjectRequest is for passing post parameters in jsonobject form. And for the header, you need to send two separate values, one for the content type for encoding.

  RequestQueue queue = Volley.newRequestQueue(this); private void makeJsonObjReq() { showProgressDialog(); Map<String, String> postParam= new HashMap<String, String>(); postParam.put("un", "xyz@gmail.com"); postParam.put("p", "somepasswordhere"); JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, Const.URL_LOGIN, new JSONObject(postParam), new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.d(TAG, response.toString()); msgResponse.setText(response.toString()); hideProgressDialog(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.d(TAG, "Error: " + error.getMessage()); hideProgressDialog(); } }) { /** * Passing some request headers * */ @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json; charset=utf-8"); return headers; } }; jsonObjReq.setTag(TAG); // Adding request to request queue queue.add(jsonObjReq); // Cancelling request /* if (queue!= null) { queue.cancelAll(TAG); } */ } 
+24
source share

All Articles