Retrofit 2.0 how to get deserialised error response.body

前端 未结 21 2278
庸人自扰
庸人自扰 2020-11-28 02:46

I\'m using Retrofit 2.0.0-beta1.

In tests i have an alternate scenario and expect error HTTP 400

I would like to have retrofit.Respons

21条回答
  •  时光说笑
    2020-11-28 03:10

    I did it this way for asynchronous calls using Retrofit 2.0-beta2:

    @Override
    public void onResponse(Response response, 
                           Retrofit retrofit) {
        if (response.isSuccess()) {
            // Do success handling here
        } else {
            try {
                MyError myError = (MyError)retrofit.responseConverter(
                        MyError.class, MyError.class.getAnnotations())
                    .convert(response.errorBody());
                // Do error handling here
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

提交回复
热议问题