you have also used retrofit 2.0 add dependency in gradle file like below ...
compile 'com.squareup.retrofit2:retrofit:2.3.0'
show every api call and data into log cat add below depencey ..
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
and after make one class for api set up like below ..
class ApiClient {
companion object {
val BASE_URL = "https://simplifiedcoding.net/demos/"
var retrofit: Retrofit? = null
fun getClient(): Retrofit? {
if (retrofit == null) {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder().apply {
readTimeout(20, TimeUnit.SECONDS)
writeTimeout(20, TimeUnit.SECONDS)
connectTimeout(20, TimeUnit.SECONDS)
addInterceptor(interceptor)
addInterceptor { chain ->
var request = chain.request()
request = request.newBuilder()
.build()
val response = chain.proceed(request)
response
}
}
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return retrofit
}
}
}
then after make interface to call different type of api like below ...
interface ApiInterface {
@GET(NetworkConstant.DATA) // hear pass your api call
fun getData(): Call>
}
make sparate class for network for api value like bellow ..
class NetworkConstant {
companion object{
const val DATA = "marvel"
}
}
then after when you call to api and getting response that time used below code ..
private fun getHeroData() {
val dialog= ProgressDialog(mContext)
showProgress(dialog)
var apiInterface: ApiInterface = ApiClient.getClient()!!.create(ApiInterface::class.java)
var hero: Call>
hero = apiInterface.getData()
hero.enqueue(object : Callback> {
override fun onFailure(call: Call>?, t: Throwable?) {
closeDialog(dialog)
Toast.makeText(mContext, t?.message, Toast.LENGTH_SHORT).show()
Log.d("Error:::",t?.message)
}
override fun onResponse(call: Call>?, response: Response>?) {
mHeroDataList.clear()
if (response != null && response.isSuccessful && response.body() != null) {
closeDialog(dialog)
mHeroDataList .addAll(response.body()!!)
setAdapter(mHeroDataList)
}
}
})
}