package com.example.trefletest import android.os.Bundle import android.widget.ImageView import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.example.trefletest.models.Plant import com.squareup.picasso.Picasso import okhttp3.OkHttpClient import retrofit2.Call import retrofit2.Callback import retrofit2.Response import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory private const val TOKEN = "cMyugCJvk-U_xoi31FVBgoEqZiuRnDYpP8fv8Vfiejg" private const val BASE_URL = "https://trefle.io/" var slug: String = "prunella-vulgaris" object ServiceBuilder { val client = OkHttpClient.Builder().build() var retrofit: Retrofit = Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .client(client) .build() fun buildService(service: Class): T { return retrofit.create(service) } } class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val nom_planta = findViewById(R.id.plant_name) as TextView //val uri_test = findViewById(R.id.uri) as TextView Por si acaso val nom_sci = findViewById(R.id.sci_name) as TextView val year = findViewById(R.id.year) as TextView val author = findViewById(R.id.author) as TextView val imagen_planta = findViewById(R.id.plantview) as ImageView nom_planta.text = ("Loading...") nom_sci.text = ("Loading...") year.text = ("Loading...") author.text = ("Loading...") val request = ServiceBuilder.buildService(TrefleService::class.java) val call = request.getPlants(slug, TOKEN) val uri = call.request().url.toString() //uri_test.text = (uri) call.enqueue(object : Callback { override fun onFailure(call: Call, t: Throwable) { Toast.makeText(this@MainActivity, t.message, Toast.LENGTH_LONG).show() } override fun onResponse(call: Call, response: Response) { if (response.isSuccessful) { val planta: Plant = response.body()!! nom_planta.text = planta.data.commonName.toString() nom_sci.text = planta.data.scientificName year.text = planta.data.year.toString() author.text = planta.data.author Picasso.get().load(planta.data.imageUrl).into(imagen_planta) } else{ Toast.makeText(this@MainActivity, response.message(), Toast.LENGTH_LONG).show() } } }) } }