Browse Source

App Backend

jose.caraballo11 3 years ago
parent
commit
31de9804d5
2 changed files with 135 additions and 0 deletions
  1. 81
    0
      Planta_Display/MainActivity.kt
  2. 54
    0
      Planta_Display/build.gradle

+ 81
- 0
Planta_Display/MainActivity.kt View File

@@ -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
+}

+ 54
- 0
Planta_Display/build.gradle View File

@@ -0,0 +1,54 @@
1
+apply plugin: 'com.android.application'
2
+apply plugin: 'kotlin-android'
3
+apply plugin: 'kotlin-android-extensions'
4
+
5
+android {
6
+    compileSdkVersion 30
7
+    buildToolsVersion "30.0.2"
8
+
9
+    compileOptions {
10
+        sourceCompatibility JavaVersion.VERSION_1_8
11
+        targetCompatibility JavaVersion.VERSION_1_8
12
+    }
13
+
14
+    kotlinOptions {
15
+        jvmTarget = JavaVersion.VERSION_1_8.toString()
16
+    }
17
+
18
+    defaultConfig {
19
+        applicationId "com.example.trefletest"
20
+        minSdkVersion 16
21
+        targetSdkVersion 30
22
+        versionCode 1
23
+        versionName "1.0"
24
+        vectorDrawables{
25
+            useSupportLibrary true
26
+        }
27
+
28
+        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
29
+    }
30
+
31
+    buildTypes {
32
+        release {
33
+            minifyEnabled false
34
+            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
35
+        }
36
+    }
37
+}
38
+
39
+dependencies {
40
+    implementation fileTree(dir: "libs", include: ["*.jar"])
41
+    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
42
+    implementation 'androidx.core:core-ktx:1.3.2'
43
+    implementation 'androidx.appcompat:appcompat:1.2.0'
44
+    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
45
+    //implementation 'org.junit.jupiter:junit-jupiter'
46
+    testImplementation 'junit:junit:4.13.1'
47
+    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
48
+    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
49
+    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
50
+    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
51
+    implementation "com.squareup.picasso:picasso:2.71828"
52
+    implementation "org.jetbrains.anko:anko-common:0.9"
53
+    implementation 'com.squareup.okhttp3:okhttp:4.9.0'
54
+}