Haytham
Haytham

Reputation: 35

Dynamic response of Retrofit

My answer Json depends on whether it succeeds or not.

Unsuccessful example:
{
    "success": false,
    "errors": {
        "email": "Could not find email address"
    }
}

Successful example:

{
    "success": true,
    "user": {
        "id": 6,
        "fname": "XXXXXX",
        "lname": "XXXXXX",
        "email": "[email protected]",
        "roles": [
            "Player"
            "Coach"
            "manager",
            "Admin"
        ]
        "date_registered": "2018-03-16T17: 49: 05.000Z"
    }
    "Token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzaWQiOiJiNjU1MDVkOGJiYzZhMTg1Y2E5MjU5NDlmNTU0OTc0MTgzM2Y2N2NiNjFjYThkMzNkMTUxY2U2MDhjMTBmNTllIiwiaWF0IjoxNTI3MjY3MzEwLCJleHAiOjE1Mjc4NzIxMTB9.p5pTlNjTsr-8N_8B3M5fW3T6PTTrcFo8D77N0WWgA3c"
}

Now, I want to have a POJO for both at the same time with retrofit.

Thank you

Edit : I just solved the problem by changing the form of JSON to :

{
"success": true,
"data": {
    "user": {
        "id": 6,
        "fname": "XXXXXXX",
        "lname": "XXXXXXXX",
        "email": "[email protected]",
        "roles": [
            "player",
            "coach",
            "manager",
            "admin"
        ],
        "activation_state": 0,
        "date_registered": "2018-03-16T17:49:05.000Z"
    },
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzaWQiOiI0MTFlZmI5Y2ExYzY1ZWFlYzQ4Yzg1ZjJkYTQwOThmODBjOTk1NWNjNjcyOTNlODI5NmI4N2RjZWY5OTMzYzljIiwiaWF0IjoxNTI4NDI0MDA1LCJleHAiOjE1Mjg0MjQ2MDl9.lTsQ867Lk78RV2ruaQFyxUNfm58bHpfzEsZnvKJQMXQ"
},
"errors": {}

}

Thank you for help

Upvotes: 2

Views: 789

Answers (2)

Ashik Azeez
Ashik Azeez

Reputation: 444

Copy your json response and past http://pojo.sodhanalibrary.com/ Then submit You will get multiple pojo class with respect to your response ,

Upvotes: 0

Moonbloom
Moonbloom

Reputation: 7918

You could simply return a String Retrofit, and then parse it manually.

If not, then you have to create a POJO object that contains all variables from both objects.

And then at runtime you check if "success" is true/false, and then try to access the underlying varaibles.

Upvotes: 1

Related Questions