Retrofit 2.0 - How to get response body for 400 Bad Request error?

前端 未结 9 1891
旧巷少年郎
旧巷少年郎 2021-01-31 02:33

So when I make a POST API call to my server, I get a 400 Bad Request error with JSON response.

{
    \"userMessage\": \"Blah\",
    \"internalMessage\": \"Bad Re         


        
9条回答
  •  心在旅途
    2021-01-31 02:55

    simply use

     if (throwable is HttpException && (throwable!!.code() == 400 || throwable!!.code()==404)){
                                   var responseBody = throwable!!.response()?.errorBody()?.string()
                                   val jsonObject = JSONObject(responseBody!!.trim())
                                   var message = jsonObject.getString("message")
                                   tvValMsg.set(message)
                                } 
    

提交回复
热议问题