Kotlin 处理来自服务的空正文响应

Kotlin 处理来自服务的空正文响应,kotlin,retrofit2,kotlinx.coroutines,Kotlin,Retrofit2,Kotlinx.coroutines,我试图通过POST呼叫一个服务,该服务向用户发送电子邮件,正文总是返回空的,我必须处理响应代码。 e、 g.204=成功 我试图用这种方式处理,但我没有成功 服务: @POST("xxxxxx") fun resendBankSlip(@Path("userId") userId: Int): Deferred<Response> 错误发生在val response=repository.sendmail(userId) 例外情况: java.lang.IllegalArgumen

我试图通过POST呼叫一个服务,该服务向用户发送电子邮件,正文总是返回空的,我必须处理响应代码。 e、 g.204=成功

我试图用这种方式处理,但我没有成功

服务:

@POST("xxxxxx")
fun resendBankSlip(@Path("userId") userId: Int): Deferred<Response>
错误发生在
val response=repository.sendmail(userId)

例外情况:

java.lang.IllegalArgumentException: 'okhttp3.Response' is not a valid response body type. Did you mean ResponseBody?
for method EmailService.sendEmail

有什么想法吗?

你可能把
okhttp3.Response
改装.Response
搞混了。请尝试在API响应中使用
2.Response
包装器,如下所示:

@POST("xxxxxx")
fun resendBankSlip(@Path("userId") userId: Int): Deferred<retrofit2.Response<Unit>>
@POST(“xxxxxx”)
fun ResendBanklip(@Path(“userId”)userId:Int):延迟
之后,您可以通过
response.code()
轻松获取响应代码


还要注意,我将
单元
作为
响应
的类型参数传递,因为您不需要主体。在其他情况下,您应该传递一个实际类型的响应体。

完美!Thanks@sma6871没错,在Kotlin中使用Unit,在Java中使用Void
@POST("xxxxxx")
fun resendBankSlip(@Path("userId") userId: Int): Deferred<retrofit2.Response<Unit>>