Android Networking: Beyond the Basics

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

Part 1: Implement Advanced Retrofit

06. Challenge: Authentication & REST Methods

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: 05. Intercept Authentication Next episode: 07. Conclusion

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

There’s not that much else to practice, as you’ve mastered nearly everything in Android networking.

Demo

Alright, this request is very similar to how the register or complete note requests work. Start by creating a new class called DeleteNoteResponse, in the model package:

@Serializable
class DeleteNoteResponse(val message: String)
@DELETE("/api/note")
fun deleteNote(@Query("id") noteId: String): Call<DeleteNoteResponse>
fun deleteTask(taskId: String, onTaskDeleted: (Result<String>) -> Unit) {
  apiService.deleteNote(taskId).enqueue(object : Callback<DeleteNoteResponse> {
    override fun onFailure(call: Call<DeleteNoteResponse>, error: Throwable) {
      onTaskDeleted(Failure(error))
    }

    override fun onResponse(call: Call<DeleteNoteResponse>,
        response: Response<DeleteNoteResponse>) {
      val completeNoteResponse = response.body()

      if (completeNoteResponse?.message == null) {
        onTaskDeleted(Failure(NullPointerException("No response!")))
      } else {
        onTaskDeleted(Success(completeNoteResponse.message))
      }
    }
  })
}
networkStatusChecker.performIfConnectedToInternet {
  remoteApi.deleteTask(taskId) { result ->
    if (result is Success) {
      taskOptionSelectedListener?.onTaskDeleted(taskId)
    }
    dismissAllowingStateLoss()
  }
}