When making API calls in Android applications, it’s common to encounter three types of responses. First, you might receive a successful response with HTTP status codes like 200/201, indicating everything is functioning correctly. Second, due to various reasons such as incorrect headers, invalid tokens, or unavailable API calls, you may get failed responses with status codes like 400/401/500, among others. Lastly, there’s a scenario where network-related or technical issues cause exceptions.
Handling all these cases can result in boilerplate code, as developers often use the Response<T> interface of the Retrofit library along with try-catch blocks at each API call. Fortunately, Kotlin’s sealed class concept provides an elegant solution to reduce boilerplate code and handle API responses effectively.