-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainAtivity.kt
75 lines (54 loc) · 2.1 KB
/
MainAtivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.example.uidesign
import android.os.Bundle
import android.widget.TextView
import org.json.JSONObject
import androidx.appcompat.app.AppCompatActivity
import java.io.IOException
//import com.google.gson.GsonBuilder
import okhttp3.*
//import com.android.volley.toolbox.Volley
import okhttp3.Request
import okhttp3.Response
//import gson.*
class MainActivity:AppCompatActivity() {
override fun onCreate(savedInstanceState:Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
fetchData()
}
private fun fetchData() {
println("OHHHHHHHHH")
val url = "https://api.thingspeak.com/channels/895784/feeds/last.json?api_key=xxxxxxxxxxxxx"
val request = Request.Builder().url(url).build()
val client = OkHttpClient()
client.newCall(request).enqueue(object: Callback {
override fun onResponse(call: Call, response: Response) {
val body = response.body?.string()
println(body)
//val gson = GsonBuilder().create()
//val homefeed = gson.fromJson(body, HomeFeed::class.java)
runOnUiThread {
val t1 = findViewById<TextView>(R.id.textView)
val t2 = findViewById<TextView>(R.id.textView1)
val t3 = findViewById<TextView>(R.id.textView2)
val t4 = findViewById<TextView>(R.id.textView3)
val json = JSONObject(body.toString())
val f1 = json.getString("field1")
val f2 = json.getString("field2")
val f3 = json.getString("field3")
val f4 = json.getString("field4")
t1.text = f1
t2.text = f2
t3.text = f3
}
}
override fun onFailure(call: Call, e: IOException) {
println("failed to execute request")
}
})
}
}
//class HomeFeed(val feeds: List<Feeds>){
//}
//class Feeds(val field1: Float, val field2: Float, val field3: Float){
//}