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

前端 未结 9 1896
旧巷少年郎
旧巷少年郎 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 03:10

    Handle ErrorResponse with your class object

    Kotlin

    val errorResponse = Gson().fromJson(response.errorBody()!!.charStream(), ErrorResponse::class.java)
    

    Java

    ErrorResponse errorResponse = new Gson().fromJson(response.errorBody.charStream(),ErrorResponse.class)
    

提交回复
热议问题