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 isstring value you can pass as parameter
Here is a code snip how you can achieve it ..
ObjectModelobjectModel=newObjectModel();
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 = newArrayList<>();
ObjectModel.Emailemail=newObjectModel.Email();
email.setPersonal("abc@gmail.com");
email.setWork("work@gmail.com");
emails.add(email);
objectModel.setEmail(emails);
Gsongson=newGson();
StringstrJsonObject= gson.toJson(objectModel);
Log.e("strJsonObject", strJsonObject);
Create ObjectModel.class
publicclassObjectModel {
String mobile_number = "";
String work_number = "";
String fax_number = "";
String first_name = "";
String last_name = "";
String website = "";
ArrayList<Email> email = newArrayList<>();
publicStringgetMobile_number() {
return mobile_number;
}
publicvoidsetMobile_number(String mobile_number) {
this.mobile_number = mobile_number;
}
publicStringgetWork_number() {
return work_number;
}
publicvoidsetWork_number(String work_number) {
this.work_number = work_number;
}
publicStringgetFax_number() {
return fax_number;
}
publicvoidsetFax_number(String fax_number) {
this.fax_number = fax_number;
}
publicStringgetFirst_name() {
return first_name;
}
publicvoidsetFirst_name(String first_name) {
this.first_name = first_name;
}
publicStringgetLast_name() {
return last_name;
}
publicvoidsetLast_name(String last_name) {
this.last_name = last_name;
}
publicStringgetWebsite() {
return website;
}
publicvoidsetWebsite(String website) {
this.website = website;
}
publicArrayList<Email> getEmail() {
return email;
}
publicvoidsetEmail(ArrayList<Email> email) {
this.email = email;
}
publicstaticclassEmail {
String work = "";
String personal = "";
publicStringgetWork() {
return work;
}
publicvoidsetWork(String work) {
this.work = work;
}
publicStringgetPersonal() {
return personal;
}
publicvoidsetPersonal(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"