Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在其他方法中调用改装并将结果返回到主线程_Android_Kotlin_Retrofit2 - Fatal编程技术网

Android 在其他方法中调用改装并将结果返回到主线程

Android 在其他方法中调用改装并将结果返回到主线程,android,kotlin,retrofit2,Android,Kotlin,Retrofit2,编写以下代码: fun getStoreTitles():List<sample> { var responseResult:List<sample> responseResult= listOf(sample("","","")) val service = getRetrofitInstance()!!.create(GetDataService::class.java) val call = servi

编写以下代码:

fun getStoreTitles():List<sample> {
        var responseResult:List<sample>
        responseResult= listOf(sample("","",""))
        val service = getRetrofitInstance()!!.create(GetDataService::class.java)
        val call = service.getAllPhotos()
        call.enqueue(object : Callback<List<sample>> {
            override fun onResponse(call: Call<List<sample>>, response: Response<List<sample>>) {
                responseResult=response.body()!!
                var t=0
            }
            override fun onFailure(call: Call<List<sample>>, t: Throwable) {
                /*progressDoalog.dismiss()*/
                //Toast.makeText(this@MainActivity, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show()
            }
        });
        return responseResult
}
fun getStoreTitles():列表{
var响应结果:列表
responseResult=listOf(示例(“,”,“))
val service=getInstance()!!.create(GetDataService::class.java)
val call=service.getAllPhotos()
排队(对象:Callback{
覆盖fun onResponse(调用:调用,响应:响应){
responseResult=response.body()!!
var t=0
}
覆盖失效时的乐趣(调用:调用,t:可丢弃){
/*progressDoalog.disclose()*/
//Toast.makeText(this@MainActivity,“出现问题…请稍后再试!”,Toast.LENGTH\u SHORT.show()
}
});
返回响应结果
}
并希望通过以下方式从主活动调用该方法:

var responseResult:List<sample>
val FrameWork=StoreTitle()
responseResult=FrameWork.getStoreTitles()
var响应结果:列表
val FrameWork=StoreTitle()
responseResult=FrameWork.getStoreTitles()

运行应用程序时,Refugation运行成功,但没有返回到responseResult,该值为空,我认为Refugation运行其他线程,这就是原因。我如何解决该问题?

更新api调用方法:

fun getStoreTitles(callback : Callback<List<sample>>) {
    var responseResult:List<sample>
    responseResult= listOf(sample("","",""))
    val service = getRetrofitInstance()!!.create(GetDataService::class.java)
    val call = service.getAllPhotos()
    call.enqueue(callback);

}
有趣的getStoreTitles(回调:回调){ var响应结果:列表 responseResult=listOf(示例(“,”,“)) val service=getInstance()!!.create(GetDataService::class.java) val call=service.getAllPhotos() call.enqueue(回调); } 你必须这样打电话:

val FrameWork=StoreTitle()
FrameWork.getStoreTitles(object : Callback<List<sample>> {
        override fun onResponse(call: Call<List<sample>>, response: Response<List<sample>>) {
         val responseResult : List<sample>? =response.body()      
         //handle your success
        }
        override fun onFailure(call: Call<List<sample>>, t: Throwable) {
             //handle your failure
        }
    })
val FrameWork=StoreTitle()
getStoreTitles(对象:回调{
覆盖fun onResponse(调用:调用,响应:响应){
val responseResult:List?=response.body()
//处理好你的成功
}
覆盖失效时的乐趣(调用:调用,t:可丢弃){
//处理你的失败
}
})

更新api调用方法:

fun getStoreTitles(callback : Callback<List<sample>>) {
    var responseResult:List<sample>
    responseResult= listOf(sample("","",""))
    val service = getRetrofitInstance()!!.create(GetDataService::class.java)
    val call = service.getAllPhotos()
    call.enqueue(callback);

}
有趣的getStoreTitles(回调:回调){ var响应结果:列表 responseResult=listOf(示例(“,”,“)) val service=getInstance()!!.create(GetDataService::class.java) val call=service.getAllPhotos() call.enqueue(回调); } 你必须这样打电话:

val FrameWork=StoreTitle()
FrameWork.getStoreTitles(object : Callback<List<sample>> {
        override fun onResponse(call: Call<List<sample>>, response: Response<List<sample>>) {
         val responseResult : List<sample>? =response.body()      
         //handle your success
        }
        override fun onFailure(call: Call<List<sample>>, t: Throwable) {
             //handle your failure
        }
    })
val FrameWork=StoreTitle()
getStoreTitles(对象:回调{
覆盖fun onResponse(调用:调用,响应:响应){
val responseResult:List?=response.body()
//处理好你的成功
}
覆盖失效时的乐趣(调用:调用,t:可丢弃){
//处理你的失败
}
})

api将在后台线程上调用,并在onResponse()方法中返回响应。因此,您将始终获得空值,因为方法
getStoreTitles()
将在api获得成功之前返回值。请使用回调来检索apiresults@RajasekaranM如何使用回调?请检查我的answerapi将在后台线程上调用,它将在onResponse()方法中返回响应。因此,您将始终获得空值,因为方法
getStoreTitles()
将在api获得成功之前返回值。请使用回调来检索apiresults@RajasekaranM如何使用回调?请检查我的回答当您可以使用改型的
回调时为什么要创建新的回调接口(您需要提供给
enqueue
方法的那一个?这是个好问题,但他在单独的类中处理了他的api调用,这是为什么,但是在您的示例中,另一个类必须实现
APICallback
,为什么不直接实现改型的
回调
?从概念上讲,他们在做相同的事情,即处理r。)回答/失败是的,你是对的,我没有考虑。谢谢:-)将更新answer@RajasekaranM在这一行-->responseResult=response.body()!!需要获取函数调用“List(…)”当您可以使用改型的
回调时,为什么要创建新的回调接口
(您需要提供给
enqueue
方法的那一个?这是个好问题,但他在单独的类中处理了他的api调用,这是为什么,但是在您的示例中,另一个类必须实现
APICallback
,为什么不直接实现改型的
回调
?从概念上讲,他们在做相同的事情,即处理r。)回答/失败是的,你是对的,我没有考虑。谢谢:-)将更新answer@RajasekaranM在这一行中-->responseResult=response.body()!!需要获取函数调用'List(…)'