Retrofit2 | #2

Зависимости:

implementation 'com.squareup.okhttp3:logging-interceptor:4.7.2'
implementation 'com.squareup.okhttp3:okhttp:4.7.2'

MainActivity.kt

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.retrofitlesson.retrofit.ProductApi
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val tv = findViewById<TextView>(R.id.tv)
val b = findViewById<Button>(R.id.button)

val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY

val client = OkHttpClient.Builder()
.addInterceptor(interceptor)
.build()

val retrofit = Retrofit.Builder()
.baseUrl("https://dummyjson.com").client(client)
.addConverterFactory(GsonConverterFactory.create()).build()
val productApi = retrofit.create(ProductApi::class.java)

b.setOnClickListener {
CoroutineScope(Dispatchers.IO).launch {
val product = productApi.getProductById(3)
runOnUiThread {
tv.text = product.title
}
}
}
}
}

Добавить комментарий

Ваш адрес email не будет опубликован.