Search code examples
error-handlinghttpresponseretrofit2eofexceptionandroidgson

EOFException at GsonResponseBodyConverter with errorBody()


I'm following DeserializeErrorBody.java as a guide

When I use Converter<ResponseBody, Error> errorConverter, Error being a POJO class for my errors, I would get an EOFException trying to do errorConverter.convert(response.errorBody()); with the log being:java.io.EOFException: End of input at line 1 column 1 path$...

If I were to log erroBody.string() it would print the error that the server sends {"message":"Incorrect user or password"}

Moreover the error code at response.code() is 401 and the response.body() is null

Why do I get this error if the body is not null?


Solution

  • I was using Observable<ResponseBody> for the Retrofit interface, like I would use Call<ResponseBody> to check only response.isSuccessful(). Turns out I need to use Observable<Response<Void>> to skip the deserialization of an empty body which causes the EOFException.