Response from xxx was null but response body type was declared as non-null

Android 使用 Kotlin、Retrofit2 请求网络,以 HTTP 的状态码 `204` 返回结果,Response from xxx was null but response body type was declared as non-null

hefengbao 发布于 2023.07.06 ,最后更新于 2023.09.18

Android 使用 Kotlin、Retrofit2 请求网络,服务器端提供的 API 对于关注、点赞之类的请求以 HTTP 的状态码 204 返回,然后处理返回值遇到错误:

Response from xxx  was null but response body type was declared as non-null

尝试了各种写法:

@PUT("posts/{postId}/like")
suspend fun likePost(
	@Path("postId") postId: Long,
	@Header("Authorization") authorization: String
)
@PUT("posts/{postId}/like")
suspend fun likePost(
	@Path("postId") postId: Long,
	@Header("Authorization") authorization: String
): Any

等等,都不能正常使用,最终的方案:

@PUT("posts/{postId}/like")
suspend fun likePost(
	@Path("postId") postId: Long,
	@Header("Authorization") authorization: String
): Response<Unit>

引入的是 import retrofit2.Response

Android    Android  

hefengbao

暂无个人简介

有0条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

来源:

https://www.8ug.icu/articles/response-from-was-null-but-response-body-type-was-declared-as-non-null-apLbXXYb6m