Android Networking: Fundamentals

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

Part 2: Implement Retrofit Basics

12. Implement a POST 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: 11. Build Retrofit Components Next episode: 13. Implement a GET Call

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: 12. Implement a POST Call

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.

Now that you’ve provided the Retrofit client and the api service, you can provide the API calls. To do this, you’ll need a few features and annotation classes from Retrofit.

@POST("/api/register")
fun registerUser(@Body request: RequestBody): Call<ResponseBody>
@POST("/api/register")
@Body request: RequestBody
Call<ResponseBody>
val body = RequestBody.create(
    MediaType.parse("application/json"), gson.toJson(userDataRequest)
)
val body = RequestBody.create(
    MediaType.parse("application/json"), gson.toJson(userDataRequest)
)

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

  override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
  }
})
val body = RequestBody.create(
    MediaType.parse("application/json"), gson.toJson(userDataRequest)
)

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

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

    onUserCreated(message, null)
  }
})
runOnUiThread{}