Search code examples
androidarraysjsonretrofitpojo

How to pass JsonArray with no name as request in retrofit body


I want to pass Json array with no name inside Retrofit body but I am getting 400 error.I would like to pass this json array with no name via pojo.Below is the json array that I want to pass.Also I am using PATCH method for this.

[{
"op": "replace",
"path": "/billing_address/line1",
"value": "some other value"}]

I am using following method and in this method I am getting the same response in logcat for list that I wanted but after passing it inside getAuthentionToken I am getting 400 error.

        Call<JSONResponse> getAuthentionToken(@Body List obj);


    JSONObject jobj = new JSONObject();
    jobj.put("op","replace");
    jobj.put("path","/billing_address/line1");
    jobj.put("value","some other value");


    List arrlist = new ArrayList();
    arrlist.add(jobj);


    apiInterface.getAuthentionToken(arrlist).enqueue(new Callback<JSONResponse>() {

Solution

  • Below is the right answer for me and its work fine.

    public class CardUpdate {

    public String op;
    public String path;
    public String value;
    
    public CardUpdate(String op, String path, String value) {
    
        this.op = op;
        this.path = path;
        this.value = value;
    }
    
    public String getOp() {
        return op;
    }
    
    public void setOp(String op) {
        this.op = op;
    }
    
    public String getPath() {
        return path;
    }
    
    public void setPath(String path) {
        this.path = path;
    }
    
    public String getValue() {
        return value;
    }
    
    public void setValue(String value) {
        this.value = value;
    }
    
    @Override
    public String toString() {
        /*return "CardUpdate{" +
                "op='" + op + '\'' +
                ", path='" + path + '\'' +
                ", value='" + value + '\'' +
                '}';*/
    
        return "{" +'"'+
                "op"  + '"'+":" +'"' +op + '"'+
                "," + '"'+"path"  + '"'+":" + '"'+ path  + '"'+
                "," + '"'+"value" + '"'+":" + '"'+ value  + '"'+
                '}';
    }
    

    }

        CardUpdate updatt = new CardUpdate("replace","/billing_address/line1","some other value");
        List<CardUpdate> cardddd = new ArrayList<CardUpdate>();
        cardddd.add(updatt);