Hi, I would like to ask, I have this kind of code ...
# getting-started
d
Hi, I would like to ask, I have this kind of code below :
Copy code
var abc2 = client2.newCall(post2).enqueue(object: Callback {
            override fun onResponse(call: Call, response: Response) {
                if(response.isSuccessful){
                val resp = response.body?.string()
                val gson = GsonBuilder().create()
                val feed: List<Feed> = gson.fromJson(resp,Array<Feed>::class.java).toList()
            }

            override fun onFailure(call: Call, e: IOException) {
                println("GAGAL")
            }
        })

    class Feed(val feed: List<Penyimpangan>)

    class Penyimpangan(val kodenik:String,val namakaryawan:String, val kodebagian:String, val tglupdate:String, val tglabsen:String,
                       val jamin:String, val jamout:String)
The problem is, when I try to see the value of feed, it throws something like this :
Copy code
[com.example.penyimpangan_idm.ApprovalpenyimpanganActivity$Feed@4cef17c]
My output from resp is like this :
Copy code
[{"kodenik":"2013129733","namakaryawan":"YASTI RIZKIA","kodebagian":"TRIE","tglupdate":"2019-12-09","tglabsen":"2019-12-09","jamin":"10:05","jamout":""}]
a
Copy code
class Penyimpangan( .. )
Doesn’t fully implement
toString()
and relies on the default Java
toString
implementation. Hence why you see that during logging. You can easily see a detailed
toString
by marking it as a
data class
instead i.e.
data class Penyimpangan()
since data class in Kotlin automatically creates a more detailed implementation of
toString
for you.
This also applies to your Feed class.
d
Hi, thanks for your answer, could you assist me further ? Sorry but, I still don't understand your explanation. Thanks
a
Your
Feed
class and
Penyimpangan
class must be both a data class. You simply need to add the
data
keyword to the class definition
data clas Feed( ... )
From what I understand, your problem is that you are trying to print out the values and the feed is showing the
toString()
of the object instance, and you wanted it to log the class properties instead. Hence why you are only missing the
data
class keyword : https://kotlinlang.org/docs/reference/data-classes.html. If this is not the exact problem, can you clarify with what exactly you are trying to see/achieve here?
d
Hi, sorry for late response, I think I've solved my problem. Your suggestion for adding data class to my existing class is correct. Thanks for helping me
👍 1