Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

java - How to pass json in request using retrofit services in android studio

How Can I pass the request data in these type of request in retrofit.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...