简体   繁体   English

改造响应异常

[英]Retrofit response exception

in the first case when it returns success true, everything works, the problem when it gets success boolean is false, then the error:在第一种情况下,当它返回成功为真时,一切正常,当它获得成功布尔值时的问题是假,然后错误:

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 26 path $ .data应为 BEGIN_OBJECT,但在第 1 行第 26 列路径 $ .data 处为 BEGIN_ARRAY

Can it be done with one reponse class?可以用一个响应类来完成吗?

Json response:
{
    "success": true,
    "data": {
        "message": "User created",
    }
}
Json response:

{
    "success": false,
    "data": [
        {
            "code": "existing_user_login",
            "message": "User Exist !"
        }
    ]
}
Code:
public class Response {
    public Boolean success;
    public Data data;

    public Boolean isSuccess() { return success; }

    public Data getData() {
        return data;
    }

    public class Data {
        public String code;
        public String message;
        public String getMessage() { return message; }
        public String getCode() { return code; }
    }

}

In success response you are getting the object在成功响应中,您正在获取对象

"data": { // object "message": "User created", "id": 98 } "data": { // 对象"message": "User created", "id": 98 }

In failure response you are getting an array在失败响应中,您将获得一个数组

"data": [ //array { "code": "existing_user_login", "message": "User Exist !" "data": [ //array { "code": "existing_user_login", "message": "User Exist!" } ] ]]

The expected failure response would be预期的失败响应是

"data": “数据”:
{ // Object "code": "existing_user_login", "message": "User Exist !" { // 对象 "code": "existing_user_login", "message": "User Exist!" } }

when response is "success": true you have get object in data当响应为"success": true您已在data获取object

Then when response is "success": false you have get array in data然后当响应为"success": false您已获得data array

This can cause you problems这可能会给你带来问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM