Getting json from retrofit's response errorBody

后端 未结 2 1611
清酒与你
清酒与你 2021-02-07 17:40

I am struggling with retrofit. When I post a request in my browser i get such a request:

And that\'s what I expect. However, when I try to parse this in my app I kept g

相关标签:
2条回答
  • 2021-02-07 18:16

    you use string(), not toString()

     ErrorResponse errorResponse = gson.fromJson(
                    response.errorBody().toString(),
                    ErrorResponse.class);
    

    to

    ErrorResponse errorResponse = gson.fromJson(
                    response.errorBody().string(),
                    ErrorResponse.class);
    
    0 讨论(0)
  • 2021-02-07 18:17

    You are using toString() in GSON's fromJson which is not a JSON content. Replace your toString() as string() which will give you the JSON body. Also make sure to use the string() method only once and save the response in a variable, because it will return empty string if you used it again.

    0 讨论(0)
提交回复
热议问题