This is a demo app for how to use Retrofit in kotlin.
<uses-permission android:name="android.permission.INTERNET"/>
//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
//logging
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
object ApiClient {
private const val BASE_URL: String = "https://jsonplaceholder.typicode.com/"
private val gson : Gson by lazy {
GsonBuilder().setLenient().create()
}
private val interceptor : HttpLoggingInterceptor by lazy {
HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
}
private val httpClient : OkHttpClient by lazy {
OkHttpClient.Builder().addInterceptor(interceptor).build()
}
private val retrofit : Retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
}
val apiService : ApiService by lazy{
retrofit.create(ApiService::class.java)
}
}
interface ApiService {
@GET("users")
fun getUsers(): Call<MutableList<User>>
}
data class User(
@SerializedName("id")
val id: Int? = null,
) : Serializable
if (isInternetAvailable()) {
getUsersData()
}
private fun getUsersData() {
showProgressBar()
ApiClient.apiService.getUsers().enqueue(object : Callback<MutableList<User>> {
override fun onFailure(call: Call<MutableList<User>>, t: Throwable) {
hideProgressBar()
Log.e("error", t.localizedMessage)
}
override fun onResponse(
call: Call<MutableList<User>>,
response: Response<MutableList<User>>
) {
hideProgressBar()
val usersResponse = response.body()
listUsers.clear()
usersResponse?.let { listUsers.addAll(it) }
adapter?.notifyDataSetChanged()
}
})
}