【问题标题】:Android Retrofit Read ErrorBody.String()Android 改造读取 ErrorBody.String()
【发布时间】:2021-08-14 08:52:24
【问题描述】:

我想使用 Retrofit 阅读错误消息。我已经尝试过 errorbody.message() 和 errorBody().string。

错误信息邮递员截图

当使用 errorBody().string() 时能够打印错误消息,但是当附加到 Toast 时,它返回空消息。 谁能帮忙解决这个问题?

 println("errr,,,,,,," + response.errorBody()!!.string()) // output Middle name mst provide
                                println("errr,,,,,,," + response.errorBody()!!.toString())  //Response{protocol=h2, code=400, message=, url="myurl"}

                                
                                Toast.makeText(context,response.errorBody()!!.string(),Toast.LENGTH_SHORT).show()
    

【问题讨论】:

    标签: android rest retrofit retrofit2


    【解决方案1】:

    我无法在您的帖子下发表评论,所以我贡献了这个答案。

    您应该在response.errorBody() 上调用一次string() 并将此字符串存储在另一个变量中,因为string() 将在您下次调用它时返回空字符串。

    //get error string
    String error = response.errorBody().string();
    
    //and use it
    println("errr,,,,", error);
    Toast.makeText(context, error, Toast.LENGTH_SHORT).show();
    

    查看this answer了解更多信息。

    【讨论】:

    • 是的,我做到了。它现在工作。感谢回复
    猜你喜欢
    • 2016-05-09
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 2016-12-24
    • 2018-07-24
    相关资源
    最近更新 更多