类型不匹配。必需:暂停()→响应<*>找到:响应<无效>

时间:2019-06-18 12:06:32

标签: android kotlin kotlin-coroutines

在我的android应用中,两种方法 getTraidersListexecuteTraderOperation必须返回类型TransportResponse

摘要:

interface TraderMonitorRestClient {

    @GET("traders/json")
    suspend fun getTraidersList(): Response<List<Trader>>

    @GET("trader/{trader_operation}")
    suspend fun executeTraderOperation(@Path("trader_operation") traderOperation: String,
                                       @Query("base") base: String,
                                       @Query("quote") quote: String,
                                       @Query("sender") sender: String,
                                       @Query("key") key: String): Response<Void>
}



 suspend fun getTraidersList(): TransportResponse = withContext(Dispatchers.IO) {
            val traderMonitorRestClient = RestClientFactory.createRestClient(TraderMonitorRestClient::class.java)
            executeOperation(traderMonitorRestClient.getTraidersList())
        }

        suspend fun executeTraderOperation(traderOperation: Trader.Operation, base: String, quote: String): TransportResponse = withContext(Dispatchers.IO) {
            val traderMonitorRestClient = RestClientFactory.createRestClient(TraderMonitorRestClient::class.java)
            val sender = BuildConfig.APPLICATION_ID + "_" + BuildConfig.VERSION_NAME
            val key = DateUtil.getDateAsString(Date(), "mmHHddMMyyyy")
            executeOperation(traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key))
        }

        suspend private fun executeOperation(someFunction: suspend () -> Response<*>): TransportResponse {
            try {
                val response: Response<*> = someFunction()
                return onResponse(response)
            } catch (e: Throwable) {
                return onNetworkFailure(e)
            }
}

但是我在这行出现编译错误:

executeOperation(traderMonitorRestClient.getTraidersList())

错误消息:

Type mismatch.
Required:
suspend () → Response<*>
Found:
Response<List<Trader>>

在这里

executeOperation(traderMonitorRestClient.executeTraderOperation(traderOperation.toString().toLowerCase(), base.trim(), quote.trim(), sender, key))

错误消息:

Type mismatch.
Required:
suspend () → Response<*>
Found:
Response<Void>

1 个答案:

答案 0 :(得分:1)

使用

executeOperation {traderMonitorRestClient.getTraidersList() }