来自 JSON 的 POJO class 用于 Retrofit2 回调
POJO class from JSON for Retrofit2 Callback
{
"success":true,
"userobject":{
"username":"user",
"email":"user@gmail.com",
"password":"user123"
}
}
这是我的JSON。我想从中创建 POJO class。现在我有这样的东西:
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 中的 "username"、"email"、"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();
}
而且它不起作用。我当然只能访问 "success" 字段。 POJO class 应该是什么样子?
userObject 是一个新对象,您应该创建一个新的class 来访问它:
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;
}
}
{
"success":true,
"userobject":{
"username":"user",
"email":"user@gmail.com",
"password":"user123"
}
}
这是我的JSON。我想从中创建 POJO class。现在我有这样的东西:
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 中的 "username"、"email"、"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();
}
而且它不起作用。我当然只能访问 "success" 字段。 POJO class 应该是什么样子?
userObject 是一个新对象,您应该创建一个新的class 来访问它:
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;
}
}