pet*_*k33 7 error-handling android response retrofit http-status-code-422
我做了一个 api 调用,它返回给我这个
响应{protocol=http/1.1, code=422, message= Unprocessable Entity, url= https://someapi/endpoint }
在日志中,连同响应我得到以下内容:
{"message":"验证失败","errors":{"email":["已被占用"]}}
我正在开发一个具有个人资料创建功能的 Android 应用程序,我想将用户重定向回来,以便在我收到此响应时可以更改其电子邮件地址,但为此我需要获取并处理“错误”消息。
如何从错误正文中获取消息?我试过这样:
响应.消息()
但我只得到
不可处理的实体
尝试如下
.subscribe(res-> {
//success case
},
t -> {
if (t instanceof HttpException) {
if (((HttpException) t).code() == 422) {
String errorResponse=((HttpException) t).response().errorBody().string();
//your validations
}
} else {
t.printStackTrace();
}
});
Run Code Online (Sandbox Code Playgroud)
我希望它会帮助你:-)
| 归档时间: |
|
| 查看次数: |
4755 次 |
| 最近记录: |