![]() PosterRemoteDataSource returns a NetworkResult by executing the handleApi function, which executes fetchPosters network requests. This is an example of the handleApi function in a data layer: You also need to handle any exceptional cases a Retrofit call may throw, like HttpException and IOException. If the response includes an error, it returns NetworkResult.Error, which contains a status code and error message. After executing the lambda function, the handleApi function returns NetworkResult.Success if the response is successful and the body data is a non-null value. The handleApi function receives an executable lambda function, which returns a Retrofit response. Let’s see how to get the NetworkResult sealed class from a Retrofit response with the handleApi function: ![]() Handling Retrofit API Responses and Exceptions This article covers modeling Retrofit responses with a sealed class, but you can use a sealed interface instead depending on your architectural design. However, sealed interfaces must have public visibility for all properties and they can expose unintended API surfaces. With a sealed interface, subclasses don’t need to be placed in the same package, which means you can use the class name as it is. If you use Kotlin version 1.5 or higher, you can also design the wrapper class with a Sealed Interface:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |