1

I am trying to send json data to a url in the format below.

      {
        “amount”:500,
        “merchant_id”:”104”,
        “narrative”:”John”,
        “reference”:”Steven”
      }

To send data to the url, one needs an authorization key as the header, I have already figured out how to set the authorization key as the header in Params, as shown below

  @Override
  public Map<String,String >getHeaders()throws AuthFailureError{
  Map<String,String >params=new HashMap<String,String>();
  params.put(“Content-Type”, “text/jsom;);
  params.put(“AuthKey”, Auth);
  return params;

but I do not know how to send the data in the particular format shown in the first instance to the url using params with volley. Please help, I am quite new to using Volley library . This is the rest of code I am currently using it however does not return any response except for an invalid json error. Meaning the format sent is not corresponding to the one desired.

   StringRequest stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
     @Override
     public void onResponse(String response) {


         Toast.makeText(Main9Activity.this, response, Toast.LENGTH_LONG).show();

     }
 },
         new Response.ErrorListener() {
             @Override
             public void onErrorResponse(VolleyError error) {
                 Toast.makeText(Main9Activity.this, error.toString(), Toast.LENGTH_LONG).show();
             }
         }){



     @Override
     protected Map<String, String> getParams() {
         HashMap<String, String> params = new HashMap<String, String>();
         params.put("amount","500");
         params.put("merchant_id", "104");

         params.put("narrative","John");
         params.put("reference", "Steven");



         return params;
     }
     @Override public Map<String,String>getHeaders()throws AuthFailureError{
         Map<String,String>headers=new HashMap<>();
         params.put("Content-Type","text/json");
         params.put("Authorization",Auth);
         return params;
     }
 };
RequestQueue requestQueue=Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
Bwalya
  • 118
  • 11

2 Answers2

1

You can refer below code snipet to post data with custom header in volley.

JSONObject jsonobject = new JSONObject();

jsonobject.put("amount", "500");
jsonobject.put("merchant_id", "104");
jsonobject.put("narrative", "John");
jsonobject.put("reference", "Steven");

  JsonObjectRequest jsonObjReq = new JsonObjectRequest(
    Request.Method.POST,url, jsonobject,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG, response.toString());
            Toast.makeText(Main9Activity.this, response.toString(), Toast.LENGTH_SHORT).show();

            hideProgressDialog();
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Toast.makeText(Main9Activity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
            hideProgressDialog();
        }
    }) {

/**
 * Passing some request headers
 * */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
     Map<String,String >headers=new HashMap<String,String>();
     headers.put(“Content-Type”, “application/json“);
     headers.put(“AuthKey”, Auth);
     return headers;
}
DHAVAL A.
  • 2,251
  • 2
  • 12
  • 27
0

You can send as below:

The data you want to send:

    Hashmap<String, Object> data = new HashMap<>();
    data.put("amount", "500");
    data.put("merchant_id", "104");
    data.put("narrative", "John");
    data.put("reference", "Steven");

RequestQueue request = Volley.newRequestQueue(context);

    JsonObjectRequest jsonobj = new JsonObjectRequest(Request.Method.POST, "your url",new JSONObject(data),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }
    ){

    };
    request.add(jsonobj);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Muazzam A.
  • 647
  • 5
  • 17