Retrofit 2.0 how to get deserialised error response.body

前端 未结 21 2229
庸人自扰
庸人自扰 2020-11-28 02:46

I\'m using Retrofit 2.0.0-beta1.

In tests i have an alternate scenario and expect error HTTP 400

I would like to have retrofit.Respons

21条回答
  •  迷失自我
    2020-11-28 03:22

    ErrorResponse is your custom response object

    Kotlin

    val gson = Gson()
    val type = object : TypeToken() {}.type
    var errorResponse: ErrorResponse? = gson.fromJson(response.errorBody()!!.charStream(), type)
    

    Java

    Gson gson = new Gson();
    Type type = new TypeToken() {}.getType();
    ErrorResponse errorResponse = gson.fromJson(response.errorBody.charStream(),type);
    

提交回复
热议问题