CPS251
Android Development
Building the RetrofitClient
The RetrofitClient file creates and configures Retrofit for the whole app. Think of it as the "network setup station" where we tell the app which website to call and how to turn JSON into Kotlin objects.
object RetrofitClient {
private const val BASE_URL = "https://api.openweathermap.org/"
val weatherApiService: WeatherApiService by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(WeatherApiService::class.java)
}
}
Code Explanation
Why Use an Object?
object RetrofitClientcreates a singleton, which means there is only one instance of this class in the app.- This is useful because we want one shared Retrofit configuration instead of creating a new network client in many places.
- Having one instance also keeps behavior consistent and easier to debug.
Base URL
private const val BASE_URL = "https://api.openweathermap.org/"stores the root API address.privatemeans this value is only used inside this file.const valmeans it is a compile-time constant and cannot change while the app runs.- Retrofit combines this base URL with endpoint paths defined in
WeatherApiService.
Lazy Initialization
val weatherApiService: WeatherApiService by lazy { ... }means the service is created only when first needed.- This can improve startup performance because we avoid creating network objects before they are needed.
- After the first creation, the same service instance is reused.
Retrofit Builder Chain
Retrofit.Builder()starts the configuration..baseUrl(BASE_URL)sets the root URL for all requests..addConverterFactory(GsonConverterFactory.create())tells Retrofit to use Gson so JSON can be converted into Kotlin data classes automatically..build()creates the configured Retrofit instance..create(WeatherApiService::class.java)generates a real implementation of your API interface.
How This Fits the Architecture
WeatherAppScreenasksRetrofitClientforweatherApiService.- That service gets passed to
WeatherRepository. - The repository calls the API through this service, and the ViewModel updates UI state.
- This keeps networking setup separate from UI code.
Learning Aids
Tips for Success
- Keep your Retrofit setup in one file so it is easy to maintain.
- Use a singleton (
object) for network clients in small to medium apps. - Always include the trailing slash in the base URL when using Retrofit.
- Use a converter factory (like Gson) when the API returns JSON.
- Use
by lazywhen an object can be created on first use.
Common Mistakes to Avoid
- Forgetting the trailing slash in
BASE_URLwhich can break endpoint building. - Creating a new Retrofit instance in multiple files instead of sharing one setup.
- Not adding a converter factory, then wondering why JSON parsing fails.
- Putting endpoint paths in this file instead of the API service interface.
- Mixing UI logic into network setup files.
Best Practices
- Keep this file focused on configuration only.
- Expose typed services (like
WeatherApiService) instead of exposing raw Retrofit everywhere. - Use clear naming so new developers can quickly find networking setup.
- In production apps, add logging interceptors and timeout configuration as needed.
- Test your API service methods with known inputs before wiring into complex UI flows.