I am Sending one request to the server with the help of retrofit. Inside the body of request I have to pass JSON. From the POJO what I am getting in the form of JSON is below
{
"id": "22",
"firstname": "abcd",
"lastname": "pqrl",
"mail": "abcd@gmail.com",
"access": true,
"has_access": true,
"iduser": 17,
"accepted": false
}
instead of this what I have to send to the body is below
{
"user": {
"id": "22",
"firstname": "abcd",
"lastname": "pqrl",
"mail": "abcd@gmail.com",
"access": true,
"has_access": true,
"iduser": 17,
"accepted": false
}
}
So what I have done is I have created JSONObject
final JSONObject parent = new JSONObject();
try {
parent.put("user",**myPojoJson**);
} catch (JSONException e) {
e.printStackTrace();
}
With this I am able to get the JSON format I want but there is some extra value added to the JSON which is nameValuePair
{
"nameValuePairs": {
"User": {
"id": "22",
"firstname": "abcd",
"lastname": "pqrl",
"mail": "abcd@gmail.com",
"access": true,
"has_access": true,
"iduser": 17,
"accepted": false
}
}
}
I checked online there are solution to use Use com.google.gson.JsonObject instead of org.json.JSONObject. But Is there any way I can still use JSONObject and able to achieve what I want ?
EDIT with adding POJO
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Vishvendu {
@SerializedName("id")
@Expose
private String id;
@SerializedName("firstname")
@Expose
private String firstname;
@SerializedName("lastname")
@Expose
private String lastname;
@SerializedName("mail")
@Expose
private String mail;
@SerializedName("access")
@Expose
private Boolean access;
@SerializedName("has_access")
@Expose
private Boolean hasAccess;
@SerializedName("iduser")
@Expose
private Integer iduser;
@SerializedName("accepted")
@Expose
private Boolean accepted;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public Boolean getAccess() {
return access;
}
public void setAccess(Boolean access) {
this.access = access;
}
public Boolean getHasAccess() {
return hasAccess;
}
public void setHasAccess(Boolean hasAccess) {
this.hasAccess = hasAccess;
}
public Integer getIduser() {
return iduser;
}
public void setIduser(Integer iduser) {
this.iduser = iduser;
}
public Boolean getAccepted() {
return accepted;
}
public void setAccepted(Boolean accepted) {
this.accepted = accepted;
}
}
This is how I am adding body to the request
interface LoginApi {
@PUT("user/abcd")
Call<abcdResponse> updateabcdStatus(@Body JSONObject parent);
}