SNM*_*SNM 8 android mvvm kotlin retrofit android-architecture-components
因此,在我删除 API 中的特定主题后,它会向我抛出此错误消息
deleteTopic 为 null,但响应正文类型被声明为非 null
该主题已成功删除,但在我的客户端中,此响应以某种方式在我的 catch 块中解析
@DELETE("topics/{id}")
suspend fun deleteTopic(@Path("id") id:String)
Run Code Online (Sandbox Code Playgroud)
override suspend fun deleteTopic(id: String): Resource<Unit> {
return Resource.Success(RetrofitClient.webservice.deleteTopic(id))
}
Run Code Online (Sandbox Code Playgroud)
fun deleteTopic(id:String) = liveData(Dispatchers.IO) {
emit(Resource.Loading())
try {
emit(repo.deleteTopic(id))
}catch (e:Exception){
emit(Resource.Failure(e))
}
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,我成功删除了服务器中的主题,但是当它返回到我的客户端时,它会转到 catch 并返回我上面提到的这个异常,响应是 204 not content,但是我该如何修复此响应成功地告诉我这个操作是否正确完成?
我很困惑,我已经在 github 上读到我必须返回Response<Unit>
,但我仍然困惑为什么我应该这样做,它还说 taht 返回将阻止我的 4xx 返回代码被抛出
Moh*_*ush 11
使用Response<Unit>
作品
参考:https ://github.com/square/retrofit/issues/3075#issuecomment-564556949
Jok*_*nas -1
我认为问题在于您的删除端点确实返回空正文响应(仅 { })。如果可以,请更改您的后端源以不这样做。否则,您可以添加自定义转换器来正确处理 4xx 状态代码。
https://github.com/square/retrofit/issues/1554#issuecomment-178633697
归档时间: |
|
查看次数: |
3774 次 |
最近记录: |