Thursday, 16 June 2016

retrofit, how to handle error

Retrofit's document doesn't give us clear instruction about how to handle errors (errors from Restful api or somebody call it backend.)

In nodejs and express if you response an error like this:

return res.send(500, "you should like peter!!!");

In your android, you can do this:

api.xxxx(a,b,c, new Callback<XXObject>() {
    @Override

    public void success(XXObject xxObject, Response response) {
        .....
    }

    @Override    public void failure(RetrofitError error) {
        String errorMessage =  new String(((TypedByteArray)error.getResponse().getBody()).getBytes());

        Log.e(getLogTag(this), "failure..." + errorMessage);
    }

});

No comments:

Post a comment