android - Retrofit implementation in Kotlin - Method Default Parameter -
i trying implement getting started retrofit android in kotlin. have made far creating instance of retrofit
class kotlin dosen't seem when try call endpoint method in interface without callback parameter. here relevant code.
the data class:
data class githubrepo(val id:int,val name:string)
the interface:
interface githubclient { @get("/users/{user}/repos") fun reposforuser(@path("user") user: string,callback: callback<list<githubrepo>> ) }
retrofit implementation:
val httpclient = okhttpclient.builder() val builder = retrofit.builder().baseurl(api_base_url) .addconverterfactory(gsonconverterfactory.create()) val retrofit = builder.client(httpclient.build()).build() val client = retrofit.create(githubclient::class.java) val call = client.reposforuser("fs-opensource") <-- error - no value passed parameter 'callback'
from tutorial:
you don’t pass callback last parameter. use client call object. once you’ve invoked .enqueue on created call object request made retrofit
how implement in kotlin?
the reposforuser
method expecting 2 arguments: string
, callback
, you're providing string
:
client.reposforuser("fs-opensource")
you can make callback "optional" doing this:
fun reposforuser(@path("user") user: string, callback: callback<list<githubrepo>>? )
i've added question mark behind callback's type in order say: it's nullable parameter. if want say, no callback default, making client call more convenient, i'd suggest provide default value callback
, so:
fun reposforuser(@path("user") user: string, callback: callback<list<githubrepo>>? = null)
be aware need access callback
in safe way now, because null. read here.
Comments
Post a Comment