Retrofit2 enqueue onResponse() in Kotlin

21,929

Solution 1

You're making an asynchronous call, so data.value will not be set until that asynchronous call resolves. However, since you are generating a MutableLiveData, you should be able to observe, which will give you an update when your asynchronous call sets the value.

Solution 2

Just use object:Callback

 accessTocken.enqueue(object : Callback<AccessToken> {
            override fun onFailure(call: Call<AccessToken>, t: Throwable) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onResponse(call: Call<AccessToken>, response: Response<AccessToken>) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

        })

Solution 3

Try

fun getNews(code: String): LiveData<List<News>>{

    val call = service.getNewsByCountry(code, Constant.API_KEY)
    var data = MutableLiveData<List<News>>()
    doAsync {
        call.enqueue(object : Callback<NewsResponse> {
            override fun onFailure(call: Call<NewsResponse>?, t: Throwable?) {
                Log.v("retrofit", "call failed")
            }

            override fun onResponse(call: Call<NewsResponse>?, response: Response<NewsResponse>?) {
                data.value = response!!.body()!!.articles
            }

        })
    }

    return data
}

If not exists doAsync try add follow anko dependency on your app/build.gralde

implementation "org.jetbrains.anko:anko-design:0.10.5"
Share:
21,929

Related videos on Youtube

Nikola
Author by

Nikola

Updated on July 05, 2022

Comments

  • Nikola
    Nikola over 1 year

    Function returns null before data.value is set in asynchronous onResponse(). How to make it first fetch data and then return that data?

    fun getNews(code: String): LiveData<List<News>>{
    
        val call = service.getNewsByCountry(code, Constant.API_KEY)
    
        var data = MutableLiveData<List<News>>()
    
        call.enqueue(object : Callback<NewsResponse> {
            override fun onFailure(call: Call<NewsResponse>?, t: Throwable?) {
                Log.v("retrofit", "call failed")
            }
    
            override fun onResponse(call: Call<NewsResponse>?, response: Response<NewsResponse>?) {
                data.value = response!!.body()!!.articles
            }
    
        })
    
        return data
    }
    
  • Nikola
    Nikola over 5 years
    I know it's asynchronous, I wanted to see if there is some elegant way to deal with these kind of situations in Kotlin. In the end I did it by observing LiveData object. Thank you.