Android转换集合数据(ArrayList)为Json格式并上传服务器

时间:2021-09-21 08:45:08

    使用Gson上传集合数据到服务器,1.最外层用 ArrayMap<String, Object> 封装;2.通过  mRequestParam.put("cmdLineIds", cmdLineIds);存入集合数据;3.用GSON.toJson转换成标准Json字符串传到服务器上。

 @Override
public Flowable<String> uploadCollectionData(String businessType, List<CmdLineId> cmdLineIds) {
// ArrayMap<String, Object> mRequestParam;
mRequestParam.clear();
mRequestParam.put("businessType", businessType);
mRequestParam.put("cmdLineIds", cmdLineIds);
Log.v("yff", "up: " + JsonUtil.map2Json(mRequestParam));// new Gson().toJson(map);
return mRequestApi.uploadCollectionData(JsonUtil.map2Json(mRequestParam)).compose(TransformerHelper.MapTransformer);
}

Log:

{
"cmdLineIds": [
{
"cmdLineId": "20180823002001"
},
{
"cmdLineId": "201808280041"
}
],
"businessType": "E4"
}