0

I want to read error message using Retrofit. I have tried errorbody.message() and errorBody().string.

Error message Postman Screenshot

enter image description here

Am able to print error message when use errorBody().string() but when append to Toast, its returning empty message. Can anyone help, to fix this?

 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()
    
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
TestOSI
  • 13
  • 3
  • Does this answer your question? [Retrofit / OkHttp3 400 Error Body Empty](https://stackoverflow.com/questions/38383401/retrofit-okhttp3-400-error-body-empty) – Vladimir Kattsyn Sep 26 '22 at 17:43

1 Answers1

0

I cannot comment under your post so I'm contributing this answer.

You should call string() on response.errorBody() once and store this string in another variable because string() will return empty string next time you call it.

//get error string
String error = response.errorBody().string();

//and use it
println("errr,,,,", error);
Toast.makeText(context, error, Toast.LENGTH_SHORT).show();

Check this answer for more info.

S. Dabrowski
  • 51
  • 1
  • 6