在我的android应用中,两种方法
getTraidersList
和executeTraderOperation
必须返回类型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>
答案 0 :(得分:1)
使用
executeOperation {traderMonitorRestClient.getTraidersList() }