美文网首页
retrofitt获取网络异常的数据

retrofitt获取网络异常的数据

作者: 黑猫警长是小白猫 | 来源:发表于2019-03-15 17:29 被阅读0次

在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获取网络异常的数据

      本文链接:https://www.haomeiwen.com/subject/churmqtx.html