Android 使用Kotlin协程对Reformation2进行错误处理

Android 使用Kotlin协程对Reformation2进行错误处理,android,kotlin,error-handling,retrofit2,kotlin-coroutines,Android,Kotlin,Error Handling,Retrofit2,Kotlin Coroutines,我了解在不使用协程时如何处理错误: @GET("user/{user}") fun getHomeData(@Path("user") user: String?): Call<HomeDataBody> fun getHomeData(id:String, callback: (Boolean, String?) -> Unit) { val call = service.getHomeData(id) call.enqueue( object : Ca

我了解在不使用协程时如何处理错误:

@GET("user/{user}")
fun getHomeData(@Path("user") user: String?): Call<HomeDataBody>



fun getHomeData(id:String, callback: (Boolean, String?) -> Unit)
{
    val call = service.getHomeData(id)
    call.enqueue( object : Callback<HomeDataBody> {
        override fun onResponse(call: Call<HomeDataBody>, response: Response<HomeDataBody>)
        {
            if (response.isSuccessful)
            {
                dataMgr.homeData = response.body()!!.user

                callback(true, null)
            }
            else
            {
                callback(false, response.message())
            }
        }

        override fun onFailure(call: Call<HomeDataBody>, t: Throwable)
        {
            callback(false, t.message)
        }

    })
}
@GET(“user/{user}”)
fun getHomeData(@Path(“用户”)用户:字符串?):调用
fun getHomeData(id:String,回调:(布尔,String?)->Unit)
{
val call=service.getHomeData(id)
排队(对象:Callback{
覆盖fun onResponse(调用:调用,响应:响应)
{
if(response.issucessful)
{
dataMgr.homeData=response.body()!!.user
回调(true,null)
}
其他的
{
回调(false,response.message())
}
}
覆盖失效时的乐趣(调用:调用,t:可丢弃)
{
回调(false,t.message)
}
})
}
但我一辈子都不知道如何使用协同程序实现这一点,这就是我对于不返回错误的协同程序所拥有的:

@GET("user/{user}")
suspend fun getHomeDataCoroutine(@Path("user") user: String?): HomeData


suspend fun getHomeDataCoroutine(id:String) : Pair<Boolean, String>
{
    val data = service.getHomeDataCoroutine(id)

    if(data != null)
    {
        dataMgr.homeData = data
    }
    else
    {
        return Pair(false, "how do i get the error message??")
    }

}
@GET(“user/{user}”)
挂起getHomeDataCoroutine(@Path(“用户”)用户:字符串?:HomeData
挂起getHomeDataCoroutine(id:String):对
{
val data=service.getHomeDataCoroutine(id)
如果(数据!=null)
{
dataMgr.homeData=数据
}
其他的
{
返回对(false,“如何获取错误消息?”)
}
}
我也尝试了这一点,但当我尝试调用service.getHomeDataCorroutine时,出现以下错误: java.lang.IllegalArgumentException:无法为类java.lang.Object创建调用适配器 对于方法RiseServiceRetro.GetHomeDataCorroutine

@GET("user/{user}")
suspend fun getHomeDataCoroutine(@Path("user") user: String?): Deferred<HomeDataBody>?

sealed class Result<out T : Any>
class Success<out T : Any>(val data: T) : Result<T>()
class Error(val exception: Throwable, val message: String =    exception.localizedMessage) : Result<Nothing>()

suspend fun getHomeDataCoroutine(id:String): Result<HomeDataBody>
{
    try {
        val response = service.getHomeDataCoroutine(id)!!.await()
        return Success(response)
    } catch (e: Exception) {
        return Error(e)
    }

}
@GET(“user/{user}”)
挂起getHomeDataCoroutine(@Path(“用户”)用户:字符串?):延迟?
密封类结果
类成功(val数据:T):结果()
类错误(val异常:Throwable,val消息:String=exception.localizedMessage):结果()
挂起getHomeDataCoroutine(id:String):结果
{
试一试{
val response=service.getHomeDataCoroutine(id)!!.await()
返回成功(响应)
}捕获(e:例外){
返回错误(e)
}
}

要在调用改装服务的
挂起
功能时处理错误,请将其包装在
try catch
块中:

@GET("user/{user}")
suspend fun getHomeDataCoroutine(@Path("user") user: String?): HomeDataBody

suspend fun getHomeDataCoroutine(id:String): Pair<Boolean, String> {
    return try {
        val data = service.getHomeDataCoroutine(id)
        dataMgr.homeData = data
        Pair(true, "")
    } catch(e: Throwable) {
        Pair(false, e.message ?: "error")
    }
}
@GET(“user/{user}”)
暂停getHomeDataCoroutine(@Path(“用户”)用户:字符串?:HomeDataBody
挂起getHomeDataCoroutine(id:String):对{
回击{
val data=service.getHomeDataCoroutine(id)
dataMgr.homeData=数据
配对(真,“”)
}捕获(e:可丢弃){
配对(错误,例如消息?:“错误”)
}
}

有帮助吗?此外,我还建议将GetHomeDataCorroutine内的逻辑转换为WHE语句。我尝试将代码调整为该答案,但在对接口进行改装时使用
suspend fun
时收到错误消息,因此可以忽略结果包装类型,即,您应该使用
HomeDataBody
而不是
Deferred?
。这可能有助于
IllegalArgumentException
。也不需要调用
await()
。据我所知,当结果不是20x时应该抛出异常,因此您应该能够在
catch
子句中处理它。你认为这里会出现什么样的错误?@Pasqal先生我尝试了你的建议,但它给出了相同的错误信息。