// 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>>
}
// 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)
}
// repository/ParkRepository.kt
import network.NationalParkServiceApi
class ParkRepository(private val api: NationalParkServiceApi) {
suspend fun fetchParks(apiKey: String) = api.getParks(apiKey)
}
// 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>>
}
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'
}
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
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()
}
}
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)
}
}
val nationalParkServiceApi = NetworkModule.provideNationalParkServiceApi()
val nationalParkServiceApi: NationalParkServiceApi = NetworkModule.provideRetrofit().create(NationalParkServiceApi::class.java)
// 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)
}