当我收到响应时,成功类完美运行。但是当它失败时,它给了我错误。
URL传入失败数据时的响应:
{
"success": -1,
"message": "Invalid username or password",
"data": ""
}
URL传入正确数据时的响应:
{
"success": 1,
"message": "User successfully logged in",
"data": {
"userId": 219,
"userName": "mp",
"picture": "219.png",
"isBo": false,
"isPo": true,
"isHm": true,
"hmProfileCompletionStatus": 3,
"poProfileCompletionStatus": 2,
"poPhoto": null,
"hmPhoto": null,
"custStripeToken": "xxx",
"accountStripeToken": "xx",
"bgcStatus": true,
"idCardStatus": true,
"isInsured": null,
"amISponsered": false,
"hmRating": null,
"poRating": null,
"email": "xxx@gmail.com"
}
}
UserResponse.java
@Getter
@Setter
public class UserResponse{
private int success;
private String message;
private User data;
}
错误
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 64 path $.data
最佳答案
这是不可能的。在你的情况下应该是这样的
{
"success": -1,
"message": "Invalid username or password",
"data": {}
}
让你的后端服务团队改成空对象,这样就可以重复使用同一个reponse对象
关于android - 当数据是字符串而不是对象时,如何在 android 中使用解析相同的 json 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45279785/