Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Implement a GET Call

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

Use Retrofit to implement a GET call, to fetch data from the API, and display it in the app. Compare the approach to HttpURLConnections.

Contributors

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

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

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

Reviews

Comments