retrofitt获取网络异常的数据

在onError的方法中添加一下代码,不管是400错误还是500错误,都可以获取到返回的数据

                    @Override
                    public void onError(Throwable e) {

                        if (e instanceof HttpException) {
                            ResponseBody responseBody = ((HttpException) e).response().errorBody();

                                if (responseBody != null) {
                                    try {
                                        String string = responseBody.string();
                                        Log.e("Tag", "PlateEditActivity onError-----" + string);
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                }
                        }
                    }

你可能感兴趣的:(retrofitt获取网络异常的数据)