Search code examples
androidandroid-volleyhubspot

Volley Post JsonArrayRequest gives client error, while testing same inputs in postman works


I am using Volley to send a Post request to HubSpot, when I sent the JSON Array object to request it gave com.android.volley.ClientError and when i log the inputs given to the JSONArrayRequest and copy them to postman it works.

tried everything but still same error.

here is what a jsonarrayobj looked when I logged it.

    [
{
    "name": "subject",
    "value": "Custom Subject"
},
{
    "name": "email",
    "value": "[email protected]"
},
{
    "name": "site",
    "value": "Site 3"
},
{
    "name": "device",
    "value": "demo-0005"
},
{
    "name": "content",
    "value": "This is content"
},
{
    "name": "hs_pipeline",
    "value": ""
},
{
    "name": "hs_pipeline_stage",
    "value": ""
}
    ]

and my code:

    JSONArray jsonArray=new JSONArray();

    Map<String,String> firstnameobj=new HashMap<String,String>();
    for (int i=0;i<7;i++) {
        firstnameobj.put("name", cool.get(i));
        firstnameobj.put("value", data[i]);
        jsonArray.put(new JSONObject(firstnameobj));
        firstnameobj.clear();
    }
    String url="https://api.hubapi.com/crm-objects/v1/objects/tickets?hapikey=MY-API-KEY";

    JsonArrayRequest jsonArrayRequest=new 
    JsonArrayRequest(Request.Method.POST, url,jsonArray, new 
    Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Toast.makeText(getContext(),"Updated 
     ",Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getContext(),"Error Adding Ticket 
    "+error.toString(),Toast.LENGTH_LONG).show();
            Log.e("error",error.toString());
        }
    }){

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> customHeader=new HashMap<String, String>();
            customHeader.put("Content-Type","application/json");
            return customHeader;
        }


    };
        requestQueue.add(jsonArrayRequest);

Solution

  • In case someone gets stuck with the same problem I resolved it BY:

      StringRequest request=new StringRequest(Request.Method.POST, url, new 
    Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(getContext(),"Created !",Toast.LENGTH_LONG).show();
    
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getContext(),"Error Adding Ticket",Toast.LENGTH_LONG).show();
    
            }
        }){
            @Override
            public byte[] getBody() throws AuthFailureError {
                return jsonArray.toString().getBytes();
            }
    
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> customHeader=new HashMap<String, String>();
                customHeader.put("Content-Type","application/json");
                return customHeader;
            }
        };
    
    
    
         requestQueue.add(request);