这是我在改造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中的对象并解析数组中的钱包信息?
答案 0 :(得分:3)
public class NewWallet {
public boolean error;
public List<Wallet> wallet;
}
让您的界面返回NewWallet
而不是ArrayList<Wallet>
答案 1 :(得分:1)
如果你的所有api只是这样改变了,那么最好使用模板。
public class ResponseDS<T> {
public boolean error;
public List<T> data;
}
现在,您可以将Wallet或任何其他对象作为T传递给具有签名的响应。 :)