0
我想在發生碰撞時獲得error
。所以,爲此我在我的班級中加入了一個變量error
。我有一個「正常身體」的變量。GSON Class with different body(Error/NoError)
public class User {
@SerializedName("error")
public Error ErrorUser;
@SerializedName("infos")
public Infos Infos;
public class Infos {
@SerializedName("login")
public String Login;
@SerializedName("lastname")
public String LastName;
@SerializedName("firstname")
public String FirstName;
@SerializedName("email")
public String Email;
@SerializedName("internal_email")
public String EmailIntern;
@SerializedName("location")
public String Location;
@SerializedName("netsoul")
public String Netsoul;
@SerializedName("studentyear")
public int SchoolYear;
public Infos(String login,
String lastname,
String firstName,
String email,
String emailIntern,
String location,
String netsoul,
int schoolYear){
this.Login = login;
this.LastName = lastname;
this.FirstName = firstName;
this.Email = email;
this.EmailIntern = emailIntern;
this.Location = location;
this.Netsoul = netsoul;
this.SchoolYear = schoolYear;
}
}
public User(Error error){
Log.d("Here", "Work");
this.ErrorUser = error;
}
public User(Infos infos){
this.Infos = infos;
}
}
隨着正常的身體(相關信息類)的工作,但是當我試圖讓錯誤的時候,它的發生,我得到user.ErrorUser on reference null object
。所以我認爲user
是空的,但我不知道爲什麼。
呼叫
ApiService.getApiService().getInformationUser(SessionManager.getInstance().getToken()).enqueue(new Callback<User>() {
@Override
public void onResponse(Response<User> response, Retrofit retrofit) {
User user = response.body();
if (user.ErrorUser == null)
Log.d("Here", user.Infos.FirstName);
else
Log.d("Here", user.ErrorUser.Message);
}
@Override
public void onFailure(Throwable t) {
}
});
錯誤類
public class Error {
@SerializedName("error")
public String Message;
@SerializedName("code")
public int Code;
public Error(String message, int code){
this.Message = message;
this.Code = code;
}
}
如果message
和code
有時倒,我需要另一個構造函數?
http://stackoverflow.com/questions/31516581/retrofit-handle-json-key-which-has-a-dynamic-structure-array-object的可能的複製 –
這是不完全一樣probleme。 –