Instantly share code, notes, and snippets.

Embed
What would you like to do?
Making a Retrofit API call with RxJava
authApi.getTokens()
.subscribe({ response: Response<AccessToken> ->
when {
response.isSuccessful && response.body() != null -> {
// Case 1: Success. We got a response with a body.
}
response.errorBody() != null -> {
// Case 2: Failure. We got an error from the backend, deserialize it.
val error = moshi.adapter(Error::class.java).fromJson(errorBody.source())
}
else -> {
// Case 3: Failure. Response didn't have a body. Show a vanilla error.
}
}
}, { t ->
// Case 4: Failure. Network error occurred, notify the user of connection troubles.
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment