-1

I need to create a method for a class which sends post request to API with parameters in the image. After that I need to get response and check if its "success"

enter image description here

Manohar
  • 22,116
  • 9
  • 108
  • 144
John Doe
  • 13
  • 4

1 Answers1

0

Here is the code , use can change params as per yours: in this getHeaders() method you need to pass your header detail and in getParams() method need to pass your body params.

StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String s) {
        ///handle response from service
    }, new ErrorResponse() {
      @Override
      public void onErrorResponse(VolleyError volleyError) {
        //handle error response
      }
  }) {
      @Override
      protected Map<String, String> getParams() throws AuthFailureError {
          Map<String, String> params = new HashMap<String, String>();
          //add params <key,value>
          return params;
      }

      @Override
      public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String,String> headers = Constants.getHeaders(context);
        // add headers <key,value>
        String credentials = USERNAME+":"+PASSWORD;
        String auth = "Basic "
                + Base64.encodeToString(credentials.getBytes(),
                Base64.NO_WRAP);
        headers.put("Authorization", auth);
        return headers;
      }
  };
 mQueue.add(request);

ref-https://gist.github.com/jchernandez/5bec1913af80e2923da8

Lovekush Vishwakarma
  • 3,035
  • 23
  • 25