Leave a rating/review
Notes: 11. Build Retrofit Components
The student materials have been reviewed and are updated as of July 2022.
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!
As with any third party library, head over to the app module’s build.gradle file, and add the following dependency:
implementation 'com.squareup.retrofit2:retrofit:2.7.2'
This will add the retrofit framework, and all its base functionality, which is what you need. Then add the following compile options, to the android section in Gradle:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
This will let Retrofit and its client use more advanced features of Java, from the newer versions. Now sync the project, and you can move onto building the dependencies! :]
Now head over to the networking package, and create a new file called RetrofitBuilders.kt.
You’ll put all the factory functions you need to build the Retrofit client and its API services here. First add the following function:
fun buildClient(): OkHttpClient =
OkHttpClient.Builder()
.build()
Retrofit uses HttpClients underneath, and by providing your own instance of the client, you can add custom configuration, when needed.
For now, the client will be the default one, but later on in the course, you’ll add custom interceptors! Next, add the Retrofit client factory function:
fun buildRetrofit(): Retrofit {
return Retrofit.Builder()
.client(buildClient())
.baseUrl(BASE_URL)
.build()
}
It uses the client from above, and adds a BASE_URL, for the base server or API endpoint. Now that you have a Retrofit client, you can build your ApiService. But you don’t have one so far. Create a new interface named RemoteApiService in the networking package:
interface RemoteApiService {
}
Then go back to the builders, and add the following code:
fun buildApiService(): RemoteApiService =
buildRetrofit().create(RemoteApiService::class.java)
This function will create an ApiService, from the class you’ve defined in the networking package.
It currently doesn’t do anything, but you’ll add behavior to it in the next episode. For now, it’s important that you’ve built the service!
Now open RemoteApi.kt. Add the RemoteApiService property to the constructor:
class RemoteApi(private val apiService: RemoteApiService) {
}
Your RemoteApi class will be a middleman between the user interface, and the actual Api service. Now, to build and pass in the Api Service, head over to the App.kt. Now add the following code, to the companion object, to expose the remoteApi to the rest of the app:
private val apiService by lazy { buildApiService() }
val remoteApi by lazy { RemoteApi(apiService) }
One final step, before venturing into rest of the episodes! You have to change all the uses of the RemoteApi, to use this one, from the App.kt file:
That’s it! Now you can move to the next episode, to implement an API call using Retrofit! :]