如何在改造 2.0 的 onResponse 上处理对象 [英] How to handle object on onResponse of retrofit 2.0

查看:43
本文介绍了如何在改造 2.0 的 onResponse 上处理对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is my logic for getting response in retrofit 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,数据是这样的:

Now they have changes the API and data is coming like this:

{
  "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中的对象并解析数组中的钱包信息?

How to handle object in ONResponse and parse wallet information in array?

推荐答案

PoJo 必须改变.例如

the PoJo has to change. E.g.

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

让你的界面返回 NewWallet 而不是 ArrayList

and let your interface return NewWallet instead of ArrayList<Wallet>

这篇关于如何在改造 2.0 的 onResponse 上处理对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆