【发布时间】:2017-05-30 17:37:38
【问题描述】:
{
"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();
}
而且它不起作用。我当然只能访问“成功”字段。 POJO 类应该是什么样子的?
【问题讨论】:
-
调用
Response.body()获取响应数据。 -
是的,我有 response.body(),但我无法访问用户名、电子邮件和密码字段
-
您在此处访问它:
response.body().getEmail()。如果结果有问题,请澄清。不要让我们猜测您的问题是什么。 -
这是 JSON 对象构建方式的影响。字段用户名、电子邮件和密码在“userobject”中,我无法访问那里。我应该更好地构建 POJO 类,但我不知道如何
标签: java android gson retrofit2 pojo