如何创建 OkHttp 所需的虚拟 ResponseBody 对象?
我现在正在传递 null
,这会导致崩溃!
请参阅:val 响应:Response
代码:
suspend fun getReviewData() = getResult {
try {
apiService.getReviewData(getCustomerId())
} catch (e: Exception) {
val response: Response<ReviewResponse> = Response.error(-1, null)
response
}
}
正如你所看到的,null在内部不接受,我必须传递这个:ResponseBody body
< a href="https://i.sstatic.net/rzc2O.png" rel="nofollow noreferrer">
I am passing null
right now, which causes a crash!
See: val response: Response<ReviewResponse> = Response.error(-1, null)
Code:
suspend fun getReviewData() = getResult {
try {
apiService.getReviewData(getCustomerId())
} catch (e: Exception) {
val response: Response<ReviewResponse> = Response.error(-1, null)
response
}
}
As you can see null is not accepting internally, and I must need pass this: ResponseBody body
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个怎么样:
What about this:
您可以创建一个像这样的结果数据类
,然后创建像这样的自定义基本响应
,并在改造中像这样分配它们
,并在调用中像这样使用它们
You can create a result data class like this
and then create your custom base response like this
and assign them like this in retrofit
and use them like this in call
取决于您的要求
addBody() 函数您没有在此处传递任何参数。需要传递参数。
看起来你的代码中缺少 api 构造。
请点击链接了解更多信息-
https://www.chillcoding.com/android-retrofit-send-http/< /a>
++ 更新
取决于您的评论,我认为您没有得到直接答案,我没有给出直接答案,它取决于您遵循的确切架构。
更多信息
java.lang.IllegalArgumentException:代码 < 400:-1
它定义了架构。
这里我建议您如何返回结果,它非常复杂,您尝试使用一些带有错误句柄和成功句柄的自定义类。
创建响应模型类并根据网络响应设置值
如成功设置成功主体和代码,否则如果失败设置错误主体和代码 ->根据响应返回。
Depend on your requirement
addBody() function you are not passing any param here. need to pass param.
look like api construction in your code missing .
please follow link to know more-
https://www.chillcoding.com/android-retrofit-send-http/
++ update
depend on your comment i think you not get direct answer , i am not give direct answer, its depend on exact architecture you following.
little bit more info
java.lang.IllegalArgumentException: code < 400: -1
its define architecture.
here i suggest you how you going to return result its quite complicated , you try with some custom class with error handle and success handle.
create response model class and set value as per network response
like success set success body and code else if fail set error body and code -> return as per response.
我看到代码看起来:
你称它为:
因此,它将失败 NullPointerException。
即使您对此行进行注释,它也会因
IllegalArgumentException
失败,因为代码小于400
。但是,您需要返回
Response
类型。您可以使用 ResponseEntity 来实现此目的:
它将完全创建 OkHttp 所需的虚拟 ResponseBody 对象。但您需要使用 ResponseEntity 而不是 Response。
或者您可以抛出异常,例如:
来自
org.springframework.web.server.ResponseStatusException
I see that the code looks:
And you call it:
Thus, it will fail by NullPointerException.
Even if you comment on this line, it will fail by
IllegalArgumentException
because the code is less than400
.However, you need to return
Response<ReviewResponse>
type.You could use
ResponseEntity
for this:It will be exactly creating dummy ResponseBody object which required by OkHttp. But you need to use
ResponseEntity
instead of Response.Or you could throw exception, like:
From
org.springframework.web.server.ResponseStatusException