Android Networking: Beyond the Basics

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

Part 1: Implement Advanced Retrofit

04. Challenge: 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: 03. Implement Logging Interceptors & Error Handling Next episode: 05. Intercept Authentication

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’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Now that you’ve added a few new concepts to your API calls, it’s time to practice them, in a challenge! :] In this challenge, you have to use the Result class, refactor a few API calls, and utilize better error handling!

Demo

Alright, let’s start with the registerUser call:

fun registerUser(userDataRequest: UserDataRequest, onUserCreated: (Result<String>) -> Unit)
remoteApi.registerUser(UserDataRequest(email, password, username)) { result ->
  if (result is Success) {
    toast(result.data)
    onRegisterSuccess()
  } else {
    onRegisterError()
  }
}
fun getTasks(onTasksReceived: (Result<List<Task>>) -> Unit)
fun addTask(addTaskRequest: AddTaskRequest, onTaskCreated: (Result<Task>) -> Unit)

fun getUserProfile(onUserProfileReceived: (Result<UserProfile>) -> Unit)

getTasks { result ->
  if (result is Failure && result.error !is NullPointerException) {
    onUserProfileReceived(Failure(result.error))
    return@getTasks
  }
  val tasks = result as Success
  
  ...
remoteApi.getTasks { result ->
  if (result is Success) {
    onTaskListReceived(result.data)
  } else {
    onGetTasksFailed()
  }
}
remoteApi.getUserProfile { result ->
  if (result is Success) {
    userEmail.text = result.data.email
    userName.text = getString(R.string.user_name_text, result.data.name)
    numberOfNotes.text = getString(R.string.number_of_notes_text, result.data.numberOfNotes)
  }
}