Advertisement

Responsive Advertisement

api implementation through kotlin

  •  sync with gradle these files

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation("com.squareup.okhttp3:logging-interceptor:4.9.1")
implementation 'com.google.code.gson:gson:2.9.0'

  • create Retrofit class

package com.example.using_kotlin_api_integretion

import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

class RetrofitClass {
private var retrofit: Retrofit? = null
private val BASEURL = "https://jsonplaceholder.typicode.com/"


fun getRetrofit(): Retrofit? {
val interceptor = HttpLoggingInterceptor()
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
val apiClient: OkHttpClient = OkHttpClient.Builder()
.addInterceptor(interceptor)
.readTimeout(8000, TimeUnit.SECONDS)
.writeTimeout(8000, TimeUnit.SECONDS)
.connectTimeout(
8000, TimeUnit.SECONDS
)
.retryOnConnectionFailure(false)
.build()
val gson = GsonBuilder()
.setLenient()
.create()
retrofit = Retrofit.Builder()
.baseUrl(BASEURL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(apiClient)
.build()
return retrofit
}
}

  • create interface
@GET("posts")
fun getresponse(): Call<ArrayList<ModelResponse?>?>?
  • create model adapter
package com.example.using_kotlin_api_integretion

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class ModelAdapter(private val mList: List<ModelResponse>) : RecyclerView.Adapter<ModelAdapter.ViewHolder>() {

// create new views
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
// inflates the card_view_design view
// that is used to hold list item
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.layout_row, parent, false)

return ViewHolder(view)
}

// binds the list items to a view
override fun onBindViewHolder(holder: ViewHolder, position: Int) {

// val ItemsViewModel = mList[position]

holder.textView1.setText(mList.get(position).getTitle())
holder.textView2.setText(mList.get(position).getBody())
holder.textView3.setText(mList.get(position).getUserId().toString())



}

// return the number of the items in the list
override fun getItemCount(): Int {
return mList.size
}

// Holds the views for adding it to image and text
class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {

val textView1: TextView = itemView.findViewById(R.id.txtid)
val textView2: TextView = itemView.findViewById(R.id.txtbody)
val textView3: TextView = itemView.findViewById(R.id.txttitle)
}
}
  • in activity
package com.example.using_kotlin_api_integretion

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.using_kotlin_api_integretion.databinding.ActivityMainBinding
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
var apiInterface: ApiInterface?=null
var apiModelResponse: ArrayList<ModelResponse>? = null
var retrofit:RetrofitClass?=null


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding= ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

apiModelResponse = java.util.ArrayList()


apiInterface= retrofit?.getRetrofit()?.create(ApiInterface::class.java)
val call= apiInterface?.getresponse();
call?.enqueue(object : Callback <ArrayList<ModelResponse>>{
override fun onResponse(
call: Call<ArrayList<ModelResponse>>,
response: Response<ArrayList<ModelResponse>>
) {
if(response.isSuccessful){
Toast.makeText(this@MainActivity,"done",Toast.LENGTH_SHORT)
}
}

override fun onFailure(call: Call<ArrayList<ModelResponse>>, t: Throwable) {
TODO("Not yet implemented")
Toast.makeText(this@MainActivity,"not done",Toast.LENGTH_SHORT)
}
})

}
}

private fun <T> Call<T>.enqueue(callback: Callback<ArrayList<ModelResponse>>) {

}

Post a Comment

0 Comments