为什么对象返回空? Android系统。 Retrofit2。
我有一个用户类:
public class User{
@SerializedName("LOGIN")
String login;
@SerializedName("PASSWORD")
String password;
@SerializedName("NAME")
String name;
@SerializedName("SURNAME")
String surname;
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
这是我的JSON:
[{
"LOGIN":"TEST",
"PASSWORD":"TEST",
"NAME":"TEST",
"SURNAME":"TEST"
}
]
界面:
public interface Link {
@GET("93sZY0Xg")
Call<List<User>> listRepos();
}
和我使用retrofit2的类LoginActivity:
public class LoginActivity extends AppCompatActivity implements Callback<List<User>>{
private Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("http://pastebin.com/raw/")
.build();
private Link service = retrofit.create(Link.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Call<List<User>> us = service.listRepos();
us.enqueue(this);
}
@Override
public void onResponse(Call<List<User>> call, Response<List<User>> response) {
System.out.println(response.body());
System.out.println(response.code());
User us = response.body().get(0);
System.out.println(us.getName());
for(User m: response.body()){
System.out.println( m.getName());
}
}
@Override
public void onFailure(Call<List<User>> call, Throwable t) {
System.out.println(t.getLocalizedMessage());
}
}
我得到了这个回复:response.code()是200;
I / System.out:[com.example.com.dataBase.User@b789a]
我在类User中尝试过getName,但所有变量都为null。
为什么值为空?哪里是我的错误?
答案 0 :(得分:3)
尝试以下操作,公开注释对于在字段中设置值非常重要
@Expose
@SerializedName("movie_id")
private String movieId;
@Expose
@SerializedName("movie_name")
private String movieName;
@Expose
@SerializedName("movie_poster")
private String moviePoster;
@Expose
@SerializedName("movie_dialog_count")
private String movieDialogCount;