{
"success":true,
"userobject":{
"username":"user",
"email":"user@gmail.com",
"password":"user123"
}
}
这是我的JSON。我想从中创建POJO类。现在我有这样的事情:
public class LoginResponse
{
@SerializedName("success")
@Expose
private Boolean success;
@SerializedName("username")
@Expose
private String username;
@SerializedName("email")
@Expose
private String email;
@SerializedName("password")
@Expose
private String password;
public Boolean getSuccess() {
return success;
}
public String getUsername() {
return username;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
}
我希望访问"用户名","电子邮件","密码"等字段。在Retrofit Callback中:
final LoginRequest loginRequest = new LoginRequest(email, password);
Call<LoginResponse> call = service.login(loginRequest);
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response)
{
if(response.isSuccessful()) {
background.setImageResource(R.drawable.bg_login_screen);
progressBar.setVisibility(View.GONE);
Toast.makeText(LoginActivity.this, response.body().getEmail(), Toast.LENGTH_SHORT).show();
}
它不起作用。我可以访问&#34;成功&#34;当然只有领域。 POJO课程应该如何?
答案 0 :(得分:2)
userObject是一个新对象,您应该创建一个新类来访问它:
public class LoginResponse {
@SerializedName("success")
@Expose
private Boolean success;
private UserObject userobject;
public Boolean getSuccess() {
return success;
}
public UserObject getUserObject() {
return userobject;
}
}
和
public class UserObject {
@SerializedName("username")
@Expose
private String username;
@SerializedName("email")
@Expose
private String email;
@SerializedName("password")
@Expose
private String password;
public String getUsername() {
return username;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
}