GSON Class 与不同 body (Error/NoError)
GSON Class with different body (Error/NoError)
我想在它发生时得到 error
。因此,为此我在 class 中为 error
放置了一个变量。我有一个 "normal body".
的变量
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;
}
}
正常的 body(信息 class)可以正常工作,但是当我尝试在错误发生时出现错误时,我得到 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) {
}
});
错误Class
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
有时是相反的,我需要另一个构造函数吗?
我用 response.errorBody()
解决了它,它包含字符串中的错误代码和消息。我用 GsonConverter()
将其转换为我的 class 错误。
try {
Error error = (Error) GsonConverterFactory.create().fromResponseBody(Error.class, Error.class.getAnnotations()).convert(response.errorBody());
Toast.makeText(this.login.getApplicationContext(), error.errorBody.Message, Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
我想在它发生时得到 error
。因此,为此我在 class 中为 error
放置了一个变量。我有一个 "normal body".
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;
}
}
正常的 body(信息 class)可以正常工作,但是当我尝试在错误发生时出现错误时,我得到 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) {
}
});
错误Class
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
有时是相反的,我需要另一个构造函数吗?
我用 response.errorBody()
解决了它,它包含字符串中的错误代码和消息。我用 GsonConverter()
将其转换为我的 class 错误。
try {
Error error = (Error) GsonConverterFactory.create().fromResponseBody(Error.class, Error.class.getAnnotations()).convert(response.errorBody());
Toast.makeText(this.login.getApplicationContext(), error.errorBody.Message, Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}