Reputation: 83
I'm trying to post my data in json format. I think I'm doing it right, but it's making the mistake: body = null, response code =417
Json data needs to post in the following format:
{
"Users": [{
'Phone': 'xxxxxxxxxxx',
'Name': 'yyyyy'
}
]
}
My code is all:
@POST("api")
@FormUrlEncoded
Call<TRList> savePost(@Field("Phone") String Phone,
@Field("Name") String Name);
}
}
public class UsersList {
@SerializedName("Users")
@Expose
private List<Post> users = null;
public List<Post> getUsers() {
return users;
}
public void setTr(List<Post> users) {
this.users = users;
}
}
public class Post {
@SerializedName("Phone")
@Expose
private String phone;
@SerializedName("AS")
@Expose
private String Name;
//getter and setter methods
}
public void sendPost(Post post){
mAPIService.savePost(post.getPhone().toString(),post.getName().toString()).enqueue(new Callback<UsersList>() {
@Override
public void onResponse(Call<UsersList> call, Response<UsersList> response) {
Log.d("requestError", "onResponse: "+ call.request().body().toString());
if(response.isSuccessful()) {
showResponse(response.body().toString());
}
}
Upvotes: 1
Views: 1196
Reputation: 618
Please check below code
First create Output class
Create class User
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class User {
@SerializedName("Phone")
@Expose
private String phone;
@SerializedName("Name")
@Expose
private String name;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Create class UserList
public class UserList {
@SerializedName("Users")
@Expose
private List<User> users = null;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
for input class you already have post class
public class Post {
@SerializedName("Phone")
@Expose
private String phone;
@SerializedName("AS")
@Expose
private String Name;
//getter and setter methods
}
on your interface create this method
@POST("api")
Call<UserList> savePost(@@Body Post post);
call service
public void sendPost(Post post){
mAPIService.savePost(post).enqueue(new Callback<UserList>() {
@Override
public void onResponse(Call<UserList> call, Response<UserList> response) {
Log.d("requestError", "onResponse: "+ call.request().body().toString());
if(response.isSuccessful()) {
showResponse(response.body().toString());
}
}
Upvotes: 1