Get the result of retrofit async call

ぃ、小莉子 提交于 2021-01-29 09:04:30

问题


I tried to use retrofit to get the response data of web api, but the result of response data seems not sync as the same.

fun fetchData(): LiveData<String> {

    val auth = Credentials.basic(name, pass)
    val request: Call<JsonElement> = webApi.fetchData()
    val response: MutableLiveData<String> = MutableLiveData()

    request.enqueue(object : Callback<JsonElement> {

        override fun onFailure(call: Call<JsonElement>, t: Throwable) {
            Log.e(TAG, "Failed to fetch token", t)
        }

        override fun onResponse(call: Call<JsonElement>, response: Response<JsonElement>) {
            response.value = response.body()
            Log.d(TAG, "response: ${response.value}")  // I can get the result of response
        }
    })

    return response  // But the function return with the null
}

You might need handler.


回答1:


The enqueue method doesn´t wait to the response so is normal the null result in your return response.

To solve this, you doesn´t need to return nothing, only put your livedata in the scope class and update the value:

class YourClass {
    private var responseMutableLiveData: MutableLiveData<String> = MutableLiveData()
    val responseLiveData: LiveData<String> 
      get() = responseMutableLiveData

    fun fetchData() {
      webApi.fetchData().enqueue(object : Callback<JsonElement> {

        override fun onFailure(call: Call<JsonElement>, t: Throwable) {
            Log.e(TAG, "Failed to fetch token", t)
        }

        override fun onResponse(call: Call<JsonElement>, response: Response<JsonElement>) {
            responseMutableLiveData.postValue(response.body())
            Log.d(TAG, "response: ${response.value}")  
        }
    })
   }
}

The livedata is observed and, when the value changes, then the other class reacts to it.



来源:https://stackoverflow.com/questions/60446998/get-the-result-of-retrofit-async-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!