[Solved] How to pass json in request using retrofit services in android studio


Main thing to consider is

    Gson gson = new Gson();
    String strJsonObject = gson.toJson(OBJECT_OF_YOUR_MODEL_CLASS);

strJsonObject is string value you can pass as parameter

Here is a code snip how you can achieve it ..

     ObjectModel objectModel = new ObjectModel();
     objectModel.setMobile_number("123456789");
     objectModel.setWork_number("12345789");
     objectModel.setFax_number("123465");
     objectModel.setFirst_name("first name");
     objectModel.setLast_name("last name");
     objectModel.setWebsite("ww.solution.com");

    ArrayList<ObjectModel.Email> emails = new ArrayList<>();
    ObjectModel.Email email = new ObjectModel.Email();
    email.setPersonal("[email protected]");
    email.setWork("[email protected]");
    emails.add(email);

    objectModel.setEmail(emails);

    Gson gson = new Gson();
    String strJsonObject = gson.toJson(objectModel);
    Log.e("strJsonObject", strJsonObject);

Create ObjectModel.class

public class ObjectModel {

String mobile_number = "";
String work_number = "";
String fax_number = "";
String first_name = "";
String last_name = "";
String website = "";
ArrayList<Email> email = new ArrayList<>();


public String getMobile_number() {
    return mobile_number;
}

public void setMobile_number(String mobile_number) {
    this.mobile_number = mobile_number;
}

public String getWork_number() {
    return work_number;
}

public void setWork_number(String work_number) {
    this.work_number = work_number;
}

public String getFax_number() {
    return fax_number;
}

public void setFax_number(String fax_number) {
    this.fax_number = fax_number;
}

public String getFirst_name() {
    return first_name;
}

public void setFirst_name(String first_name) {
    this.first_name = first_name;
}

public String getLast_name() {
    return last_name;
}

public void setLast_name(String last_name) {
    this.last_name = last_name;
}

public String getWebsite() {
    return website;
}

public void setWebsite(String website) {
    this.website = website;
}

public ArrayList<Email> getEmail() {
    return email;
}

public void setEmail(ArrayList<Email> email) {
    this.email = email;
}

public static class Email {
    String work = "";
    String personal = "";

    public String getWork() {
        return work;
    }

    public void setWork(String work) {
        this.work = work;
    }

    public String getPersonal() {
        return personal;
    }

    public void setPersonal(String personal) {
        this.personal = personal;
    }
}

}

1

solved How to pass json in request using retrofit services in android studio