简体   繁体   中英

What is the correct way to show error response in Retrofit2?

I've created Model class to handle my Retrofit2 Callback:

public class ModelSendPhone {

@Expose
@SerializedName("code")
private int code;
@Expose
@SerializedName("user_id")
private int user_id;

public int getCode() {
    return code;
}

public void setCode(int code) {
    this.code = code;
}

public int getUser_id() {
    return user_id;
}

public void setUser_id(int user_id) {
    this.user_id = user_id;
}

And here is my Call code:

Call<ModelSendCode> sendCodeCall = getRetrofit().sendCode(params);
sendCodeCall.enqueue(this);

Now I noticed that if I enter wrong @Params,the response is giving me error 422 and JSON response body with message something like:

{"name":"Unprocessable entity","message":"Wrong params","code":0,"status":422,"type":"yii\\web\\HttpException"}

My question is how should I show this "message" in Toast inside onResponse method? My onResponse method not parsing it as I'm implementing Callback<ModelSendCode> .

Thanks in-advance

Your model can be look like this and all will fine just check status in response.

public class ModelSendPhone implements Serializable
{
    private String message;

    private String status;

    private String name;

    private int user_id;

    private int code;

    private String type;

    public String getMessage ()
    {
        return message;
    }

    public void setMessage (String message)
    {
        this.message = message;
    }

    public String getStatus ()
    {
        return status;
    }

    public void setStatus (String status)
    {
        this.status = status;
    }

    public String getName ()
    {
        return name;
    }

    public void setName (String name)
    {
        this.name = name;
    }

    public int getUser_id ()
    {
        return user_id;
    }

    public void setUser_id (int user_id)
    {
        this.user_id = user_id;
    }

    public int getCode ()
    {
        return code;
    }

    public void setCode (int code)
    {
        this.code = code;
    }

    public String getType ()
    {
        return type;
    }

    public void setType (String type)
    {
        this.type = type;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [message = "+message+", status = "+status+", name = "+name+", user_id = "+user_id+", code = "+code+", type = "+type+"]";
    }
}

In your onResponse method you'll get a response object of type

Response<ModelSendCode>

It's an HTTP response. According to the Square docs

You can simply do response.message() to get the message from the HTTP response. You can also check if it's been successful by using response.isSucessful() . Have you tried that?

Thanks for the comments, but I found what I needed. response.errorBody().string() That's the correct way according to the docs , to get default response on any error, now I will convert it to Json and parse it as I wanted

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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