Network Requests with Retrofit in Android

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

Lesson 02: Meet Retrofit

Demo

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 file and add the following dependency:

implementation "com.squareup.retrofit2:retrofit:2.9.0"
interface MovieDiaryApiService {

  @POST("user/register")
  fun registerUser()

  @POST("user/login")
  fun loginUser()

  @GET("user")
  fun getProfile()

  @GET("movies")
  fun getMovies()

  @POST("movies")
  fun makeEntry()
}
private const val BASE_URL = "https://http-api-93211a10efe2.herokuapp.com/"
private fun buildClient(): OkHttpClient = OkHttpClient.Builder().build()
private fun buildRetrofit(): Retrofit = Retrofit.Builder()
  .baseUrl(BASE_URL)
  .client(buildClient())
  .build()
fun buildMovieDiaryService(): MovieDiaryApiService =
  buildRetrofit().create(MovieDiaryApiService::class.java)
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Conclusion