Android Networking: Fundamentals

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

Part 2: Implement Retrofit Basics

14. Challenge: Create Retrofit Calls

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: 13. Implement a GET Call Next episode: 15. Add Queries to 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: 14. Challenge: Create Retrofit Calls

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

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

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