Reputation: 689
I have JSON
that looks like this:
[{"name":"John","age":30,"vehicle":"bicycle"},
{"name":"Bob","age":32,"vehicle":"walking"}]
The interface
@GET("lnjb8")
fun findPosts(): Call<List<RetrofitVariables>>
The variables for deserialisation:
class RetrofitVariables(name: String, age: String, vehicle: String) {}
And in the MainActivity()
:
val myRetrofit = Retrofit.Builder()
.baseUrl("https://api.myjson.com/bins/")
.addConverterFactory(GsonConverterFactory.create())
.build()
val repository = myRetrofit.create(RetrofitCalls::class.java)
val call = repository.findPosts()
...followed by the asynchronous call:
call.enqueue(object: Callback<List<RetrofitVariables>> {
override fun onResponse(call: Call<List<RetrofitVariables>>?, response: Response<List<RetrofitVariables>>?) {
val jsonVariables = response!!.body()
//THIS SHOULD PRODUCE THE PROPER DATA
jsonVariables!!.forEach { println("TAG testing" + it) }
}
override fun onFailure(call: Call<List<RetrofitVariables>>?, t: Throwable?) {
println("TAG FAIL")
}
})
jsonVariables!!.forEach { println("TAG testing" + it) }
should give me the data, but instead it just gives testingcom.slothmode.retrofitexercise.RetrofitVariables@83098f7
Obviously, it collects the data since it gets a response, but I must have missed how to display it properly. In previous projects I have used value
as in x.value.toString() but I don't see how that apply here.
How do I display the proper data?
Upvotes: 0
Views: 131
Reputation: 1640
It is being shown that way because you haven't implemented toString()
method. You can either implement toString()
in RetrofitVariables
class or you can declare RetrofitVariables
as data class
and have it implemented by the compiler.
Upvotes: 1