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.

Transcript: 04. Challenge: Error Handling

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!

You have to change the registerUser, getTasks, addTask and the getUserProfile calls, to utilize the Result from the previous episode. Make sure to check that everything works, by using the logging interceptor, and debugging the calls! :]

That’s it! Now pause the video, and solve the challenge! Then once you’re done, unpause the video, and compare the two solutions! Good luck! :]

Demo

Alright, let’s start with the registerUser call:

fun registerUser(userDataRequest: UserDataRequest, onUserCreated: (Result<String>) -> Unit)

Change the onResponse & failure

Now head over to RegisterActivity, and change the code there:

remoteApi.registerUser(UserDataRequest(email, password, username)) { result ->
  if (result is Success) {
    toast(result.data)
    onRegisterSuccess()
  } else {
    onRegisterError()
  }
}

Now you’re using just the result, and the code looks better and easier to read. Go back to the RemoteApi, and head over to the getTasks call. Change the code as such:

fun getTasks(onTasksReceived: (Result<List<Task>>) -> Unit)

You’ll have to change two things here, the NotesFragment, and the getUserProfile call. Let’s change the rest of the API calls here first, and then move to the usages.

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
  
  ...

Now head over to the NotesFragment, and change the function to use a result instead:

remoteApi.getTasks { result ->
  if (result is Success) {
    onTaskListReceived(result.data)
  } else {
    onGetTasksFailed()
  }
}

And once more, head over to the ProfileFragment, to do the same:

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)
  }
}

And for the last request, head over to the AddTaskDialogFragment and implement the result. Finally, run the project, and make sure everything works as before! :]