Android Networking: Fundamentals

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

Part 2: Implement Retrofit Basics

11. Build Retrofit Components

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

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

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

As you’ve learned, to use Retrofit, you need to first build a Retrofit client, providing the Base URL, and other configuration you might need. Let’s see how to do that!

implementation 'com.squareup.retrofit2:retrofit:2.7.2'
compileOptions {
  sourceCompatibility JavaVersion.VERSION_1_8
  targetCompatibility JavaVersion.VERSION_1_8
}
fun buildClient(): OkHttpClient = 
  OkHttpClient.Builder()
    .build()
fun buildRetrofit(): Retrofit {
  return Retrofit.Builder()
      .client(buildClient())
      .baseUrl(BASE_URL)
      .build()
}
interface RemoteApiService {
}
fun buildApiService(): RemoteApiService = 
  buildRetrofit().create(RemoteApiService::class.java)
class RemoteApi(private val apiService: RemoteApiService) {
}
private val apiService by lazy { buildApiService() }

val remoteApi by lazy { RemoteApi(apiService) }