(菜鸟级文章,大神绕道通行,勿喷~)移动端开发中难免会上传各种格式的数据给后台,前两天一个刚工作的学妹问我如何上传数组给后台,这么一个简单的问题警醒我应该做个笔记记录一下我上传过的数据格式:
总而言之,不管是什么数据类型,搞不定的就新建个模型类,然后再重写的 toString()方法中写需要上传的数据格式就行了
一.数组格式,内含字典,如下图所示
![](https://img.haomeiwen.com/i3989735/1a984498a997c131.png)
![](https://img.haomeiwen.com/i3989735/57541db191bbe968.png)
做法:
首先 新建Bean类SupplyProductsBean .class
public class SupplyProductsBean {
public String product_id;//产品id
public String num;//数量
public SupplyProductsBean(String product_id, String num) {
this.product_id = product_id;
this.num = num;
}
@Override
public String toString() {
return "{" +
"\"product_id\":\"" + product_id + '\"' +
",\"num\":\"" + num + '\"' +
'}';
}
}
然后 添加Bean类数据
List<SupplyProductsBean> supplyList = new ArrayList<>();
for (int i = 0; i < cartList.size(); i++) {
supplyList.add(new SupplyProductsBean(cartList.get(i).product_id, productlist.get(i).buy_num + ""));
}
最后通过map上传(用网络框架实现八种上传方式都行)即可
Map<String, Object> map = new HashMap<String, Object>();
map.put("supply_products", list);
传上去的数据结果:
![](https://img.haomeiwen.com/i3989735/d512bc238e8aee31.png)
二.数组或字符串
![](https://img.haomeiwen.com/i3989735/f72d8cc1cf4ad5df.png)
做法:
val map = HashMap<String, Any?>()
map.put("market_id", market_id)
if (list != null) {
val ints = IntArray(list.size)
for (i in list.indices) {
ints[i] = list[i].market_product_id
}
map.put("market_product_id_data", ints)
}
网友评论