Good morning everyone. I would like to know how t...
# android
a
Good morning everyone. I would like to know how to use Retrofit for an ArrayList. I tried to find solution from SO. Most of them are Java I managed to convert them into Kotlin but I am still getting error.
Copy code
MainActivity.kt
fun retrieveApiData() {
	val vibeApi: VibeApi
	var vibeList = mutableListOf<Vibes>()

	val retrofit: Retrofit = Retrofit.Builder()
								.baseUrl(Constants.baseUrl)
								.addConverterFactory(MoshiConverterFactory.create())
								.build()

	vibeApi = retrofit.create(VibeApi::class.java)

	val call = vibeApi.getVibes()

	// Object is not abstract and does not implement abstract member public abstract fun onResponse(call: Call<kotlin.collections.ArrayList<VibeList> /* = java.util.ArrayList<VibeList> */!>!, response: Response<kotlin.collections.ArrayList<VibeList> /* = java.util.ArrayList<VibeList> */!>!): Unit defined in retrofit2.Callback
	call.enqueue(object: Callback<ArrayList<VibeList>> {
		
		// Modifier 'override' is not applicable to 'local function'
		override fun onResponse(call: Call<Vibes>?, response: Response<Vibes>?) {
			var vibes = response?.body()
		}

		// Modifier 'override' is not applicable to 'local function'
		override fun onFailure(call: Call<Vibes>?, t: Throwable?) {
			Log.d("Vibe Adapter", "Failure")
		}
	})
}
Copy code
DataClass

data class Vibes(@Json(name = "result") val vibeList: ArrayList<VibeList>)

data class VibeList(
        @Json(name = "id") val id: Integer?,
        @Json(name = "url") val url: String?,
        @Json(name = "author") val author: String?)
This is really not a Kotlin related question, so it shouldnt be posted here
t
it might be kotlin related. all of the params in the
onResponse
and
onFailure
are marked as null which was probably done by the IDE auto complete. but the compiler is probably respecting the non null by default behavior that the squares put into these libraries last year. because of the mis match of nullability the compiler is saying the method signatures don't match thus the
override is invalid
and the
object doesn't implement fun
errors
@Ayden check the nullability on your callback impl. also why not just use
List
you are using val and immutable types everywhere else so I would guess you don't need or want the mutable list interface exposed there.
a
@trevjones thank you for the suggestion. I will try it out.
Sorry I just came back. XD
@rkeazor thank you for moving my question to appropriate channel.
r
@trevjones @Ayden Well the actual issue is that he is overriding methods with the wrong signature. It Callback<T> so the types should be types of T. In other words if you have object:Callable<ArrayList<VibeList>>
than you should override methods override fun onResponse(call: Call<ArrayList<VibeList>>? , response :response<ArrayList<VibeList>>?)
and the same thing for onFailure.
The link i posted explains this in detail…
The reason why its nullable , is more or less a platform type issue. In other word the underlying java implementation is not doing a a null check,so the Kotlin compiler cannot prove that the parameters are not nullable
a
@rkeazorthanks Robert. I managed to solve my issue.
But I don't know what actually going on. XD
It just works.
r
😂
just watch the link I sent you