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.

Challenge: Create Retrofit Calls

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

Practice using Retrofit and its annotations, to implement a new API call. Practice parsing the response body.

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.

To practice retrofitting API calls, you’ll implement two API calls in this challenge using Retrofit. You have to change the Login request to use Retrofit, instead of the manual HttpURLConnection, and you have to implement a new API call, the getUserProfile call.

@POST("/api/login")
fun loginUser(@Body request: RequestBody): Call<ResponseBody>

@GET("/api/user/profile")
fun getMyProfile(@Header("Authorization") token: String): Call<ResponseBody>
val body = RequestBody.create(
    MediaType.parse("application/json"), gson.toJson(userDataRequest)
)

apiService.loginUser(body).enqueue(object : Callback<ResponseBody> {
  override fun onFailure(call: Call<ResponseBody>, error: Throwable) {
    onUserLoggedIn(null, error)
  }

  override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
    val jsonBody = response.body()?.string()
    if (jsonBody == null) {
      onUserLoggedIn(null, NullPointerException("No response body!"))
      return
    }

    val loginResponse = gson.fromJson(jsonBody, LoginResponse::class.java)

    if (loginResponse == null || loginResponse.token.isNullOrEmpty()) {
      onUserLoggedIn(null, NullPointerException("No response body!"))
    } else {
      onUserLoggedIn(loginResponse.token, null)
    }
  }
})
getTasks { tasks, error ->
}
getTasks { tasks, error ->
  if (error != null && error !is NullPointerException) {
    onUserProfileReceived(null, error)
    return@getTasks
  }
}
getTasks { tasks, error ->
  if (error != null && error !is NullPointerException) {
    onUserProfileReceived(null, error)
    return@getTasks
  }

  apiService.getMyProfile(App.getToken()).enqueue(object : Callback<ResponseBody> {
    override fun onFailure(call: Call<ResponseBody>, error: Throwable) {
      onUserProfileReceived(null, error)
    }

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

      if (jsonBody == null) {
        onUserProfileReceived(null, error)
        return
      }

      val userProfileResponse = gson.fromJson(jsonBody, UserProfileResponse::class.java)

      if (userProfileResponse?.email == null || userProfileResponse.name == null) {
        onUserProfileReceived(null, error)
      } else {
        onUserProfileReceived(
            UserProfile(userProfileResponse.email, userProfileResponse.name, tasks.size), null
        )
      }
    }
  })
}
runOnUiThread {
}

Reviews

Comments