Android Networking: Fundamentals

Sep 6 2022 · Kotlin 1.6, Android 12, Android Studio Chipmunk | 2021.2.1 Patch 1

Part 2: Implement Retrofit Basics

13. Implement a GET Call

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: 12. Implement a POST Call Next episode: 14. Challenge: Create Retrofit Calls

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.

Notes: 13. Implement a GET Call

The student materials have been reviewed and are updated as of July 2022.

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

After implementing that POST call to register a user, it’s time to implement a GET call, to fetch the tasks or notes from the server.

@GET("/api/note")
fun getNotes(@Header("Authorization") token: String): Call<ResponseBody>
apiService.getNotes(App.getToken()).enqueue(object : Callback<ResponseBody> {
  override fun onFailure(call: Call<ResponseBody>, error: Throwable) {
  }

  override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
  }
})
apiService.getNotes(App.getToken()).enqueue(object : Callback<ResponseBody> {
  override fun onFailure(call: Call<ResponseBody>, error: Throwable) {
    onTasksReceived(emptyList(), error)
  }

  override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
    val jsonBody = response.body()?.string()

    if (jsonBody == null) {
      onTasksReceived(emptyList(), NullPointerException("No data available!"))
      return
    }

    val data = gson.fromJson(jsonBody, GetTasksResponse::class.java)

    if (data != null && data.notes.isNotEmpty()) {
      onTasksReceived(data.notes.filter { !it.isCompleted }, null)
    } else {
      onTasksReceived(emptyList(), NullPointerException("No data available!"))
    }
  }
})
activity?.runOnUiThread {
}