【问题标题】:How to handle object on onResponse of retrofit 2.0如何在改造 2.0 的 onResponse 上处理对象
【发布时间】:2016-03-12 09:50:56
【问题描述】:

这是我在改造 2.0 中获得响应的逻辑

call.enqueue(new Callback<ArrayList<Wallet>>() {
  @Override
  public void onResponse(Response<ArrayList<Wallet>> response, Retrofit retrofit) {

      if (response.isSuccess()) {
          // use response data and do some fancy stuff :)
         loading.dismiss();
          ArrayList<Wallet> orders = response.body();
          Utility.displayToast("Wallet size is" + orders.size());

      } else {

      }
  }

});


Data format from rest API is like this:

[
{
  "description": "Cashback",
  "amount": "20.00",
  "type": "1",
  "date": "11/03/2016"
},
{
  "description": "CASH BACK",
  "amount": "12.00",
  "type": "1",
  "date": "05/03/2016"
}
]

现在他们对 API 进行了更改,数据如下所示:

{
  "error": false,
  "wallet": [
    {
      "description": "Cashback",
      "amount": "20.00",
      "type": "1",
      "date": "11/03/2016"
    },
    {
      "description": "CASH BACK",
      "amount": "12.00",
      "type": "1",
      "date": "05/03/2016"
    }
  ]
}    

如何处理ONResponse中的对象并解析数组中的钱包信息?

【问题讨论】:

    标签: android retrofit2


    【解决方案1】:

    PoJo 必须改变。例如

    public class NewWallet {
        public boolean error;
        public List<Wallet> wallet;
    }
    

    让你的界面返回NewWallet而不是ArrayList&lt;Wallet&gt;

    【讨论】:

      【解决方案2】:

      如果你的所有 api 都只是这样改变,那么最好使用模板。

      public class ResponseDS<T> {
          public boolean error;
          public List<T> data;
      }
      

      现在您可以将 Wallet 或任何其他对象作为 T 传递给带有签名的响应。 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-02
        • 1970-01-01
        • 2021-03-27
        • 2020-03-06
        • 1970-01-01
        • 2019-06-03
        • 1970-01-01
        相关资源
        最近更新 更多