{
"success":true,
"userobject":{
"username":"user",
"email":"[email protected]",
"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;
}
}
我想访问翻新回调中的诸如“用户名”,“电子邮件”,“密码”之类的字段:
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();
}
而且它不起作用。我只能访问“成功”字段。POJO类应如何显示?
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;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句