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 POST Call

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

Use Retrofit and its annotations, to implement an API request in the api service. Learn about the Call and Callback types in Retrofit.

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.

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

Reviews

Comments