|
@@ -0,0 +1,81 @@
|
|
1
|
+package com.example.trefletest
|
|
2
|
+
|
|
3
|
+import android.os.Bundle
|
|
4
|
+import android.widget.ImageView
|
|
5
|
+import android.widget.TextView
|
|
6
|
+import android.widget.Toast
|
|
7
|
+import androidx.appcompat.app.AppCompatActivity
|
|
8
|
+import com.example.trefletest.models.Plant
|
|
9
|
+import com.squareup.picasso.Picasso
|
|
10
|
+import okhttp3.OkHttpClient
|
|
11
|
+import retrofit2.Call
|
|
12
|
+import retrofit2.Callback
|
|
13
|
+import retrofit2.Response
|
|
14
|
+import retrofit2.Retrofit
|
|
15
|
+import retrofit2.converter.gson.GsonConverterFactory
|
|
16
|
+
|
|
17
|
+private const val TOKEN = "cMyugCJvk-U_xoi31FVBgoEqZiuRnDYpP8fv8Vfiejg"
|
|
18
|
+private const val BASE_URL = "https://trefle.io/"
|
|
19
|
+var slug: String = "prunella-vulgaris"
|
|
20
|
+
|
|
21
|
+object ServiceBuilder {
|
|
22
|
+
|
|
23
|
+ val client = OkHttpClient.Builder().build()
|
|
24
|
+
|
|
25
|
+ var retrofit: Retrofit = Retrofit.Builder()
|
|
26
|
+ .baseUrl(BASE_URL)
|
|
27
|
+ .addConverterFactory(GsonConverterFactory.create())
|
|
28
|
+ .client(client)
|
|
29
|
+ .build()
|
|
30
|
+
|
|
31
|
+ fun <T> buildService(service: Class<T>): T {
|
|
32
|
+ return retrofit.create(service)
|
|
33
|
+ }
|
|
34
|
+}
|
|
35
|
+
|
|
36
|
+class MainActivity : AppCompatActivity() {
|
|
37
|
+ override fun onCreate(savedInstanceState: Bundle?) {
|
|
38
|
+ super.onCreate(savedInstanceState)
|
|
39
|
+ setContentView(R.layout.activity_main)
|
|
40
|
+ val nom_planta = findViewById(R.id.plant_name) as TextView
|
|
41
|
+ //val uri_test = findViewById(R.id.uri) as TextView Por si acaso
|
|
42
|
+ val nom_sci = findViewById(R.id.sci_name) as TextView
|
|
43
|
+ val year = findViewById(R.id.year) as TextView
|
|
44
|
+ val author = findViewById(R.id.author) as TextView
|
|
45
|
+ val imagen_planta = findViewById(R.id.plantview) as ImageView
|
|
46
|
+
|
|
47
|
+ nom_planta.text = ("Loading...")
|
|
48
|
+ nom_sci.text = ("Loading...")
|
|
49
|
+ year.text = ("Loading...")
|
|
50
|
+ author.text = ("Loading...")
|
|
51
|
+
|
|
52
|
+ val request = ServiceBuilder.buildService(TrefleService::class.java)
|
|
53
|
+ val call = request.getPlants(slug, TOKEN)
|
|
54
|
+ val uri = call.request().url.toString()
|
|
55
|
+
|
|
56
|
+ //uri_test.text = (uri)
|
|
57
|
+
|
|
58
|
+ call.enqueue(object : Callback<Plant> {
|
|
59
|
+ override fun onFailure(call: Call<Plant>, t: Throwable) {
|
|
60
|
+ Toast.makeText(this@MainActivity, t.message, Toast.LENGTH_LONG).show()
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ override fun onResponse(call: Call<Plant>, response: Response<Plant>) {
|
|
64
|
+ if (response.isSuccessful) {
|
|
65
|
+ val planta: Plant = response.body()!!
|
|
66
|
+ nom_planta.text = planta.data.commonName.toString()
|
|
67
|
+ nom_sci.text = planta.data.scientificName
|
|
68
|
+ year.text = planta.data.year.toString()
|
|
69
|
+ author.text = planta.data.author
|
|
70
|
+ Picasso.get().load(planta.data.imageUrl).into(imagen_planta)
|
|
71
|
+ }
|
|
72
|
+ else{
|
|
73
|
+ Toast.makeText(this@MainActivity, response.message(), Toast.LENGTH_LONG).show()
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ }
|
|
77
|
+ })
|
|
78
|
+
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+}
|