Network Requests with Retrofit in Android

Jun 5 2024 · Kotlin 1.9.22, Android 14, Android Studio Hedgehog | 2023.1.1

Lesson 04: Parse JSON with Moshi

Demo 2

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

To start, open the app module’s build.gradle and add the dependency for the Moshi converter:

implementation "com.squareup.retrofit2:converter-moshi:2.9.0"
.addConverterFactory(MoshiConverterFactory.create())
@POST("user/register")
fun registerUser(@Body body: RegisterBody): Call<Unit>

@POST("user/login")
fun loginUser(@Body body: RegisterBody): Call<LoginResponse>

@GET("user")
fun getProfile(): Call<User>

@GET("sampleMovies")
fun getMovies(): Call<List<MovieReview>>

@POST("movies")
fun postReview(@Body movieReview: MovieReview): Call<MovieReview>
@JsonClass(generateAdapter = true)
data class LoginResponse(val token: String)
val body = RegisterBody(username, password, email)

apiService.registerUser(body).enqueue(object : Callback<Unit> {
  override fun onResponse(call: Call<Unit>, response: Response<Unit>) {
    if (response.isSuccessful) {
      onResponse("Success", null)
    } else {
      onResponse(null, Throwable(response.message()))
    }
  }

  override fun onFailure(call: Call<Unit>, error: Throwable) {
    onResponse(null, error)
  }
})
fun loginUser(
  username: String,
  password: String,
  onResponse: (LoginResponse?, Throwable?) -> Unit
) {
  val loginBody = RegisterBody(username, password)

  apiService.loginUser(loginBody).enqueue(object : Callback<LoginResponse> {
    override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
      if (response.isSuccessful) {
        onResponse(response.body(), null)
      } else {
        onResponse(null, Throwable(response.errorBody()?.string()))
      }
    }

    override fun onFailure(call: Call<LoginResponse>, error: Throwable) {
      onResponse(null, error)
    }
  })
}
fun getMovies(onResponse: (List<MovieReview>?, Throwable?) -> Unit) {
  apiService.getMovies().enqueue(object : Callback<List<MovieReview>> {
    override fun onResponse(call: Call<List<MovieReview>>, response: Response<List<MovieReview>>) {
      if (response.isSuccessful) {
        onResponse(response.body(), null)
      } else {
        onResponse(null, Throwable(response.errorBody()?.string()))
      }
    }

    override fun onFailure(call: Call<List<MovieReview>>, error: Throwable) {
      onResponse(null, error)
    }
  })
}
@Composable
fun LoginScreen(
  movieDiaryApi: MovieDiaryApi,
  connectivityChecker: ConnectivityChecker,
  onLogin: (LoginResponse) -> Unit,
  onRegisterTapped: () -> Unit,
)
movieDiaryApi.loginUser(username, password) { loginResponse, error ->
  if (loginResponse == null) {
    screenScope.launch {
      scaffoldState.snackbarHostState.showSnackbar(error?.message ?: "")
    }
  } else {
    onLogin(loginResponse)
  }
}
onLogin = { loginResponse ->
  App.saveUserToken(loginResponse.token)
  userLoggedIn = true
  currentScreen = Screens.MOVIES
}
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction 2 Next: Conclusion