Android Networking: Beyond the Basics

Sep 8 2022 · Kotlin 1.7.10, Android 12, Android Studio Chipmunk

Part 1: Implement Advanced Retrofit

03. Implement Logging Interceptors & Error Handling

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 02. Use Different Parsers Next episode: 04. Challenge: Error Handling

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

When dealing with requests, you can always use more information to help you either see where the problem lies in case you receive an error, or fail to parse some data.

Demo

To add the logging interceptor, first, open the app level build.gradle, and add the following dependency:

implementation 'com.squareup.okhttp3:logging-interceptor:4.10.0'
.addInterceptor(HttpLoggingInterceptor().apply {
  level = HttpLoggingInterceptor.Level.BODY
})
sealed class Result<out T : Any>

data class Success<out T : Any>(val data: T) : Result<T>()

data class Failure(val error: Throwable?) : Result<Nothing>()
fun loginUser(userDataRequest: UserDataRequest, onUserLoggedIn: (Result<String>) -> Unit)
onUserLoggedIn(Failure(error))

...

if (loginResponse == null || loginResponse.token.isNullOrEmpty()) {
  onUserLoggedIn(Failure(NullPointerException("No response body!")))
} else {
  onUserLoggedIn(Success(loginResponse.token))
}
remoteApi.loginUser(userDataRequest) { result ->
  if (result is Success) {
    onLoginSuccess(result.data)
  } else {
    showLoginError()
  }
}