我有这个JSON正文,我想将其发送到服务器。
{
"username" : "aa",
"email" : "aa@email.com",
"telephone" : 1234565165,
"plainPassword" : {
"first" : "aa",
"second" : "aa"
}
}
我使用以下实体:
User.java
public class User {
private int id;
private String username;
private long telephone;
private String email;
private String image_name;
private int activation;
private int status;
private String randomcode;
}
PlainPassword.java
public class PlainPassword {
private String first;
private String second;
}
我的API接口类
@POST("registerREST")
Call<Object> addUser(@Body Object user);
我这样使用它:
Call<Object> call = services.addUser(u);
call.enqueue(new Callback<Object>() {
@Override
public void onResponse(Call<Object> call, Response<Object> response) {
if(response.isSuccessful()){
LinkedTreeMap treemap = (LinkedTreeMap) response.body();
RegisterResponse review = new RegisterResponse();
double d = Double.parseDouble(treemap.get("userId").toString()) ;
review.setUserId((int) d );
if(review.getUserId() == 0) {
} else {
}
}
}
@Override
public void onFailure(Call<Object> call, Throwable t) {
}
});
答案 0 :(得分:3)
您必须执行的操作是: 有两个物体
public class Example {
@SerializedName("username")
@Expose
private String username;
@SerializedName("email")
@Expose
private String email;
@SerializedName("telephone")
@Expose
private Integer telephone;
@SerializedName("plainPassword")
@Expose
private PlainPassword plainPassword;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getTelephone() {
return telephone;
}
public void setTelephone(Integer telephone) {
this.telephone = telephone;
}
public PlainPassword getPlainPassword() {
return plainPassword;
}
public void setPlainPassword(PlainPassword plainPassword) {
this.plainPassword = plainPassword;
}
}
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class PlainPassword {
@SerializedName("first")
@Expose
private String first;
@SerializedName("second")
@Expose
private String second;
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getSecond() {
return second;
}
public void setSecond(String second) {
this.second = second;
}
}
然后您需要以以下格式发送此邮件:
@POST("registerREST")
Call<ReturningObject> addUser(@Body Example user);
如果您对此有更多疑问,我邀请您检查此网站:https://futurestud.io/tutorials/retrofit-getting-started-and-android-client
答案 1 :(得分:0)
public class test {
public String username;
public String email;
public long telephone;
public PlainPassword plainPassword;
public test(String username, String email, long telephone, PlainPassword plainPassword) {
this.username = username;
this.email = email;
this.telephone = telephone;
this.plainPassword = plainPassword;
}
public class PlainPassword{
public String first;
public String second;
}
}
use upper class for data post
在此之后
(您的plainpass类)PlainPassword pp = new PlainPassword(“ yourfirst”,“ yoursecond”);
(您的主类名称)test ta =新测试(“用户名”,“电子邮件”,“ yourteliphonno”,pp)
通话后
呼叫= = services.addUser(ta);