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.

Build Retrofit Components

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

Build the OkHttpClient, Retrofit and the RemoteApiService instance, to prepare everything for API call implementation.

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.

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

Reviews

Comments