【问题标题】:How to get the body of a Retrofit Call in the callback如何在回调中获取改造调用的主体
【发布时间】:2021-02-09 16:01:53
【问题描述】:

假设我有这个虚拟 API

@POST("somepath/setSomething")
Call<ExecuteResponse> setSomething(@Body ExecuteInput input);

还有这个回调

Callback<ExecuteResponse> callBack = new Callback<ExecuteResponse>() {
        @Override
        public void onResponse(Call<ExecuteResponse> call, Response<ExecuteResponse> response) {};

如何访问调用主体并取回我的 ExecuteInput 对象?

【问题讨论】:

  • 如果您想获取原始请求,可以尝试call.request()
  • @christoandrew 那段代码不会给我返回对象,而是 OkHttp3 的 Request 对象

标签: android retrofit2 okhttp


【解决方案1】:

你可以做response.getBody(),你会得到你在Response&lt;???&gt;中指定的对象。在这种情况下,ExecuteResponse

当然,我假设您在对象中定义了@SerializedName,或者您在ExecuteResponse 模型类中具有正确的字段名称。

【讨论】:

  • 我问的是如何获取请求,而不是响应
猜你喜欢
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-06
  • 1970-01-01
  • 1970-01-01
  • 2019-05-21
  • 1970-01-01
相关资源
最近更新 更多