How a Custom Retrofit Converter Factory Prevents Crashes on Empty API Responses
Android's Retrofit library crashes with an EOFException when a server returns an empty response body under HTTP status codes like 200 or 201, because the JSON converter attempts to parse a blank stream. Retrofit natively handles empty bodies only for 204 No Content and 205 Reset Content status codes, bypassing the converter entirely for those cases. Many real-world APIs, however, return empty bodies with 200 or 201 responses, exposing a gap in Retrofit's default behavior. A common client-side fix involves building a custom NullOnEmptyConverterFactory that intercepts empty response streams before they reach the JSON converter and returns null instead. Registering this factory ahead of the JSON converter in Retrofit's ordered converter list ensures the workaround applies consistently across all API calls.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in