How To Pass Json In Request Using Retrofit Services In Android Studio
Solution 1:
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("abc@gmail.com");
email.setWork("work@gmail.com");
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;
}
}
}
Solution 2:
Creating data request using son body is very simple while you using retrofit.
Follow the below steps to complete this in easy manner.
Copy the Json body code and paste in this website as shown in the below image
Add the Package name and class name in the respective fields and select the options as I shown in the image.
Click preview, then it will generate the model class with given parameters like in the below image.
Modelclass Preview used for Retrofit
Then close the Window and click on zip button, it will generate the .zip folder with the necessary files. Copy those files and add in your project.
Then In your Retrofit Interface class, call the JSON body file like this
@Headers("Content-Type: application/json") @POST(APIs.REGISTER) Call<ModelResponse> registerUser(@Body ModelBody modelBody);
If you have any queries, you can reach me here.
Post a Comment for "How To Pass Json In Request Using Retrofit Services In Android Studio"