Sending post parameters with JsonObjectRequest volley to active campaign api

193 views Asked by At

I am adding contacts to active campaign api but the request is not sending the post parameters.The parameters are being sent from postman but with volley its not working. I have tried sending params from the constructor also but no progress. Here is the code.

Map<String, String> params = new HashMap();
params.put("email", "[email protected]");
params.put("p[1]", "1");
//JSONObject parameters = new JSONObject(params);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
url="https://brumano.api-us1.com/admin/api.php?api_key=key&api_action=contact_add&api_output=json";
Log.d("url",url);
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,null, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        Log.d("url",response.toString());
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        // TODO Auto-generated method stub
        Log.d("url",error.toString());
        error.printStackTrace();
    }
}){
    @Override
    public byte[] getBody() {
        HashMap<String, String> params2 = new HashMap<String, String>();
        params2.put("email", "[email protected]");
        params2.put("p[1]", "1");
        return new JSONObject(params2).toString().getBytes();
    }

        @Override
        public String getBodyContentType() {
            return "application/x-www-form-urlencoded;";
        }

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params2 = new HashMap<String, String>();
            params2.put("email", "[email protected]");
            params2.put("p[1]", "1");
            return params2;
        }
    }
};
queue.add(jsObjRequest);
2

There are 2 answers

2
Ramesh sambu On BEST ANSWER

Try this using StringRequest

StringRequest jsonObjRequest = new StringRequest(Request.Method.POST,
       "https://brumano.api-us1.com/admin/api.php?api_key=key&api_action=contact_add&api_output=json",
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("url",response.toString());

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("volley", "Error: " + error.getMessage());

            }
        }) {

    @Override
    public String getBodyContentType() {
        return "application/x-www-form-urlencoded; charset=UTF-8";
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> params = new HashMap<String, String>();
        params.put("email", "[email protected]");
        params.put("p[1]", "1");
        return params;
    }

};

queue.add(jsonObjRequest);
0
hemen On

check this, with stringRequest like this

   //Tested on PostMan
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("userName", "[email protected]");
            params.put("password", "qwerty");
            System.out.println(params);
            return params;
        }

Now if your server expects json, then make jsonRequest
 JSONObject jsonObjectBody = new JSONObject();
        jsonObjectBody.put("userName", "[email protected]");
        jsonObjectBody.put("password", "qwerty");