Skip to content

Instantly share code, notes, and snippets.

@naturalwarren
Last active January 23, 2019 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naturalwarren/9535273faa8a629015083db09da54b78 to your computer and use it in GitHub Desktop.
Save naturalwarren/9535273faa8a629015083db09da54b78 to your computer and use it in GitHub Desktop.
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