Java 用于回调的JSON中的POJO类

Java 用于回调的JSON中的POJO类,java,android,gson,retrofit2,pojo,Java,Android,Gson,Retrofit2,Pojo,这是我的JSON。我想从中创建POJO类。现在我有这样的东西: { "success":true, "userobject":{ "username":"user", "email":"user@gmail.com", "password":"user123" } } 我想在改装回调中访问“用户名”、“电子邮件”、“密码”等字段: public class LoginResponse { @SerializedName("s

这是我的JSON。我想从中创建POJO类。现在我有这样的东西:

{
   "success":true,
   "userobject":{
       "username":"user",
       "email":"user@gmail.com",
       "password":"user123"
    }
}
我想在改装回调中访问“用户名”、“电子邮件”、“密码”等字段:

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=新登录请求(电子邮件、密码);
Call=service.login(loginRequest);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应)
{
if(response.issusccessful()){
setImageResource(R.drawable.bg\u登录屏幕);
progressBar.setVisibility(View.GONE);
Toast.makeText(LoginActivity.this,response.body().getEmail(),Toast.LENGTH\u SHORT.show();
}

它不起作用。我当然只能访问“成功”字段。POJO类应该是什么样子?

userObject是一个新对象,您应该创建一个新类来访问它:

 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();
                }


userObject是一个新对象,您应该创建一个新类来访问它:

 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();
                }


通过调用检索响应数据。是的,我有response.body(),但无法访问用户名、电子邮件和密码字段您正在此处访问它:
response.body().getEmail()
。如果结果有问题,请澄清。不要让我们猜你的问题是什么。这是JSON对象构建方式的影响。用户名、电子邮件和密码字段在“userobject”中,我无法访问。我应该更好地使用POJO类,但我不知道如何通过调用检索响应数据。是的,我有响应。body()但是我无法访问用户名、电子邮件和密码字段您在这里访问它:
response.body().getEmail()
。如果结果有问题,请澄清。不要让我们猜测您的问题是什么。这是JSON对象构建方式的影响。用户名、电子邮件和密码字段位于“userobject”中我不能进入那里。我应该更好地学习POJO课程,但我不知道怎么做
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;
    }
}