fromJson返回空的android改装和gson
当我将json错误响应从改型转换为对象时,我将得到null JSON字符串:fromJson返回空的android改装和gson,android,retrofit,gson,Android,Retrofit,Gson,当我将json错误响应从改型转换为对象时,我将得到null JSON字符串: {"messageType":1,"hasErrors":true,"isSuccess":false,"message":"Invalid username or password"} 我创建的类: public class ValidationContainer { @SerializedName("messageType") @Expose private int MessageType; @Serial
{"messageType":1,"hasErrors":true,"isSuccess":false,"message":"Invalid username or password"}
我创建的类:
public class ValidationContainer {
@SerializedName("messageType")
@Expose
private int MessageType;
@SerializedName("hasErrors")
@Expose
private Boolean HasErrors;
@SerializedName("isSuccess")
@Expose
private Boolean IsSuccess;
@SerializedName("message")
@Expose
private String Message;
public ValidationContainer() {
}
public String getMessage() {
return Message;
}
public void setMessage(String message) {
this.Message = message;
}
}
我的代码:
public void onResponse(Call<UserDTO> call, Response<UserDTO> response) {
if (response.isSuccessful()) {
//TODO: Save response user properties to shared constants
Intent intent = new Intent(getBaseContext(), MainActivity.class);
startActivity(intent);
finish();
} else if (response.code() == 400) {
Gson gson = new GsonBuilder().create();
ValidationContainer container = new ValidationContainer();
try {
Log.e(Tag, response.errorBody().string());
container = gson.fromJson(response.errorBody().string(), ValidationContainer.class);
Log.e(Tag, container.getMessage());
} catch (IOException e) {
Log.e(Tag, e.getMessage());
}
}
}
我做错了什么?根据以下链接找到了解决方案:
public void onResponse(调用、响应){
if(response.issusccessful()){
//TODO:将响应用户属性保存到共享常量
Intent Intent=new Intent(getBaseContext(),MainActivity.class);
星触觉(意向);
完成();
}else if(response.code()==400){
转换器=
responseBodyConverter(ValidationContainer.class,新注释[0]);
试一试{
ValidationContainer error=converter.convert(response.errorBody());
Toast.makeText(getBaseContext(),error.getMessage(),Toast.LENGTH_LONG.show();
}捕获(例外e){
Log.d(标记,例如getMessage());
}
}
}
请粘贴实际代码,因为我认为gson解析不需要任何IO异常进行解析,因为我可以看到您正在使用它
删除
if(response.code()==400)
请在日志中发布错误消息最初我认为您必须实现可序列化接口
public class ValidationContainer implements Serializable {
}
尝试将
UserDTO
更改为ValidationContainer
@DheerubhaiBansal,但我想转换错误,而不是成功响应。直接将库版本更改为com.google.code.gson:gson:2.6.2
发布实际代码是什么意思?问题中发布的代码就是实际代码
public void onResponse(Call<UserDTO> call, Response<UserDTO> response) {
if (response.isSuccessful()) {
//TODO: Save response user properties to shared constants
Intent intent = new Intent(getBaseContext(), MainActivity.class);
startActivity(intent);
finish();
} else if (response.code() == 400) {
Converter<ResponseBody, ValidationContainer> converter =
retrofit.responseBodyConverter(ValidationContainer.class, new Annotation[0]);
try {
ValidationContainer error = converter.convert(response.errorBody());
Toast.makeText(getBaseContext(), error.getMessage(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.d(Tag, e.getMessage());
}
}
}
public class ValidationContainer implements Serializable {
}