done: Step 1: Create a package named "network" and add an interface named "NationalParkServiceApi."
// network/NationalParkServiceApi.kt
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Query
interface NationalParkServiceApi {
@GET("parks")
suspend fun getParks(
@Query("api_key") apiKey: String
): Response<List<Park>>
}
Step 2: Define a function named "getParks" with the necessary annotations for Retrofit.
The function "getParks" is already defined in the "NationalParkServiceApi" interface above.
To create a NetworkModule object in Android Kotlin that provides a Retrofit instance, follow these steps:
Add the necessary dependencies to your app's build.gradle file: dependencies {
// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
// OkHttp
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'
}
Create a new Kotlin file named NetworkModule.kt in your network package. Import the necessary classes and interfaces:
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
Create the NetworkModule object with a function to provide a Retrofit instance: object NetworkModule {
private const val BASE_URL = "https://your-api-base-url.com/"
fun provideRetrofit(): Retrofit {
val loggingInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.build()
return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build()
}
}
How do I make an object is it like making a file
In Kotlin, you usually create objects by creating a new Kotlin file (.kt) and defining the object within that file. You can follow these steps to create the NetworkModule object:
In your Android Studio project, right-click on the package where you want to create the NetworkModule object, and select New -> Kotlin File/Class. Name the file "NetworkModule" and click "OK". Inside the newly created "NetworkModule.kt" file, you'll define the NetworkModule object like this:
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
object NetworkModule {
private const val BASE_URL = "https://your-base-url.com/"
private val retrofit: Retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
fun provideNationalParkServiceApi(): NationalParkServiceApi {
return retrofit.create(NationalParkServiceApi::class.java)
}
}
Replace "" with the base URL of your API. Now, with the NetworkModule object created, you can easily create an instance of the NationalParkServiceApi interface by calling the provideNationalParkServiceApi() function:
val nationalParkServiceApi = NetworkModule.provideNationalParkServiceApi()
You can place the above line of code in your ViewModel or Repository, depending on your architecture, to create and use the NationalParkServiceApi instance.
Replace https://your-api-base-url.com/ with your actual API base URL.
Now, you can use the NetworkModule object to provide a Retrofit instance and create an instance of your NationalParkServiceApi interface:
val nationalParkServiceApi: NationalParkServiceApi = NetworkModule.provideRetrofit().create(NationalParkServiceApi::class.java)
This will allow you to make API calls using the nationalParkServiceApi instance.
Step 3: Create a "NetworkModule" object to provide a Retrofit instance.
// network/NetworkModule.kt
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
object NetworkModule {
private const val BASE_URL = "https://developer.nps.gov/api/v1/"
private val okHttpClient = OkHttpClient.Builder().build()
private val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build()
val nationalParkServiceApi: NationalParkServiceApi = retrofit.create(NationalParkServiceApi::class.java)
}