I am new to Kotlin and I find library Klaxon to parse JSON. I can not find how to execute url(http://localhost:8080/items/2), read JSON string and save data to variables and print them to console. CreatedAt and UpdatedAt I do not need to save.
JSON from url:
{
"brand": "Ferrero",
"name": "Nutella",
"healthy": false,
"date": "2017-03-14T00:00:00.000Z",
"id": 2,
"createdAt": "2018-03-14T13:33:22.000Z",
"updatedAt": "2018-03-20T21:23:44.000Z"
}
Code:
class DetailItem : AppCompatActivity() {
var itemId : String = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail_item)
itemId = intent.getStringExtra("itemId")
}
override fun onResume() {
super.onResume()
example()
}
private fun parse(name: String) : Any{
val cls = Parser::class.java
val inputStream = cls.getResourceAsStream(name)!!
return Parser().parse(inputStream)!!
}
private fun example(){
val url = "http://192.168.99.100:8080/items/" + itemId
val obj = parse("url") as JsonObject
val brand = obj.string("brand")
val name = obj.string("name")
println(brand + name)
}