Android Retrofit入队方法未返回值 [英] Android Retrofit enqueue method does not return value

查看:226
本文介绍了Android Retrofit入队方法未返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

fun getSurvey() : MutableList<SurveyMaster>{
        //Retrofit creates implementation for this method getSurveyList()
        val call : Call<List<SurveyMaster>> = retrofitPlaceHolderAPI.getSurveyList("bearer "+customIDToken)

        val surveyList =   mutableListOf<SurveyMaster>()

        //enqueue - executing on the background thread, to execute asynchronously
        call.enqueue(object : Callback<List<SurveyMaster>>{
            override fun onFailure(call: Call<List<SurveyMaster>>, t: Throwable) {
                Log.e("Error", t.message)
                //Log.e("Error", t.localizedMessage)
            }

            override fun onResponse(call: Call<List<SurveyMaster>>, response: Response<List<SurveyMaster>>) {
                if (!response.isSuccessful){
                    Log.e("SurveyList Error", "code: "+response.code())
                    return
                }

                val surveys = response.body()

                for(element in surveys!!){
                    surveyList.add(element)
                }
            }
        })
        return surveyList
    }

Enqueue方法在后台线程上运行,但是如何在enqueue方法中返回列表.上面的代码返回空列表.预先感谢

Enqueue method runs on background thread but how to return the list inside enqueue method. The above code returns empty list. Thanks in advance

推荐答案

就像您已经提到的那样,排队将在后台线程上执行此API调用,因此您的函数将始终返回空列表,因为目前尚未收到来自API,因此您提到的此循环:-

Like you already mentioned that enqueue will execute this API call on a background thread so your function will always return empty list as right now no response has been received from the API ,so this loop that you've mentioned:-

for(element in surveys!!){
    surveyList.add(element)
}

此循环将稍后运行(当收到API的响应时),同时您返回了此刻为空的列表.此处您无法直接从该方法返回数据.

This loop will run later on(when response from API is received) meanwhile you've returned the list which is empty at this instant.Here you cannot directly return data from the method.

您可以通过接口使用回调方法.由于您已经以列表的形式获取了响应,因此无需创建单独的列表并将数据添加到列表中.

You can use callback approach using interfaces. As you're already getting the response as a list no need to create a separate list and add the data into it.

fun getSurvey(onGetSurveyListener: OnGetSurveyListener){

    val call : Call<List<SurveyMaster>> = retrofitPlaceHolderAPI.getSurveyList("bearer "+customIDToken)

    val surveyList =   mutableListOf<SurveyMaster>()

    //enqueue - executing on the background thread, to execute asynchronously
    call.enqueue(object : Callback<List<SurveyMaster>>{
        override fun onFailure(call: Call<List<SurveyMaster>>, t: Throwable) {
            Log.e("Error", t.message)
            onGetSurveyListener.onGetSurveyFailure(t.localizedMessage)
            //Log.e("Error", t.localizedMessage)
        }

        override fun onResponse(call: Call<List<SurveyMaster>>, response: Response<List<SurveyMaster>>) {
            if (!response.isSuccessful){
                Log.e("SurveyList Error", "code: "+response.code())
                return
            }
            response.body?.let{list->
               onGetSurveyListener.onGetSurveySuccess(list)
            }
        }
    })
}

interface OnGetSurveyListener {
    fun onGetSurveySuccess(surveyList:List<SurveyMaster>)
    fun onGetSurveyFailure(errorMessage: String)
}

现在通过成功和失败回调,您可以处理两种情况.

Now here with the success and failure callbacks you can handle both cases.

这篇关于Android Retrofit入队方法未返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆