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()
    
shirley
  • 10,698
  • 8
  • 25
  • 48
TestOSI
  • 13
  • 3

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
  • 36
  • 2
  • 6