I want to post following JSON,
{
"name": {
"firstName": "f_name",
"lastName": "l_name"
},
"password": "mypassword123",
"email": "[email protected]"
}
register method inside Interface is,
@POST("user/createuser")
Call<RegisterResponseModel> register(@Body RegisterModel body);
using register method like,
Name name = new Name("f_name", "l_name");
RegisterModel registerModel = new RegisterModel(name, "mypassword123", "[email protected]");
Call<RegisterResponseModel> res = apiService.register(registerModel);
but couldn't reach what I want, Please help me to achieve what I need. Thanks in advance
You can do like this first create a Pojo class like
class SendDataModel{
private String email;
private Name name;
private String password;
public String getEmail ()
{
return email;
}
public void setEmail (String email)
{
this.email = email;
}
public Name getName ()
{
return name;
}
public void setName (Name name)
{
this.name = name;
}
public String getPassword ()
{
return password;
}
public void setPassword (String password)
{
this.password = password;
}
@Override
public String toString()
{
return "ClassPojo [email = "+email+", name = "+name+", password = "+password+"]";
}
}
and other Pojo Class like
class Name{
private String lastName;
private String firstName;
public String getLastName ()
{
return lastName;
}
public void setLastName (String lastName)
{
this.lastName = lastName;
}
public String getFirstName ()
{
return firstName;
}
public void setFirstName (String firstName)
{
this.firstName = firstName;
}
@Override
public String toString()
{
return "ClassPojo [lastName = "+lastName+", firstName = "+firstName+"]";
}
}
and set your first and last name like
Name name = new Name();
name.setFirstName();
name.setLastName();
SendDataModel sendDatamodel=new SendDataModel();
sendDatamodel.setName(name);
sendDatamodel.setEmail("yourEmail")
sendDatamodel.setPassword("yourPassword");
and pass your sendDatamodel to your request.
Call<RegisterResponseModel> res = apiService.register(sendDatamodel);
res.enqueue(new Callback<RegisterResponseModel>() {
@Override
public void onResponse(Call<RegisterResponseModel> call,
Response<RegisterResponseModel> response) {
}
@Override
public void onFailure(Call<RegisterResponseModel> call, Throwable t)
{
// Log error here since request failed
Log.e(TAG, t.toString());
}
});
I was facing the same problem when trying to POST data in raw
form only
@POST("your_url_here")
Call<Object> getUser(@Body Map<String, String> body);
class
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface apiInterface = retrofit.create(ApiInterface.class);
try {
Map<String, String> requestBody = new HashMap<>();
requestBody.put("email", "[email protected]");
requestBody.put("password", "123678");
Call<Object> call=apiInterface.getUser(requestBody);
call.enqueue(new Callback<Object>() {
@Override
public void onResponse(Call<Object> call, Response<Object> response) {
try {
JSONObject object=new JSONObject(new Gson().toJson(response.body()));
Log.e("TAG", "onResponse: "+object );
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<Object> call, Throwable t) {
}
});
} catch (Exception e) {
e.printStackTrace();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With