Sachin Suryavanshi
Sachin Suryavanshi

Reputation: 128

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>() {

Upvotes: 0

Views: 1708

Answers (2)

Wajid
Wajid

Reputation: 2341

If you are sending data over request body your implementation should be like this:

  1. Define model according to fields (CaseSensitive "Name" -> String Name etc )
  2. set your api function also like that

@POST("/api/geo/getLoc") public void getFriendsLocation(@Body YourClass classObject, Callback<JsonElement> response);

  1. Use directly your created class object on post request

    getFriendsLocation(yourClassObjectThatIncludesFields, new Callback .... );

If your sending data over params You can do this with Gson.
1. Lets say you have a class that have fields like id , number and FriendNumber.Define a function :

public static Map<String, Object> getMapFromObject(Object o) {
    Gson gson = new Gson();
    Type stringObjectMap = new TypeToken<Map<String, Object>>() {
     }.getType();
    return gson.fromJson(gson.toJson(o), stringObjectMap);
}

2. set your api function also like that

@POST("/api/geo/getLoc")
public void getFriendsLocation(@QueryMap Map<String, Object>,     Callback<JsonElement> response);
  1. When you are sending post request create object from your fields call this function like below here

    getFriendsLocation(getMapFromObject(yourClassObjectThatIncludesFields), new Callback .... );

I didnt write whole code which includes class definition and Callback function because they are up to your customization. I assume that you need to send over body so try the first way.

Upvotes: 1

Sachin Suryavanshi
Sachin Suryavanshi

Reputation: 128

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);

Upvotes: 0

Related Questions