我有一个下面的对象数组要在服务调用中传递.
[ { "ParkingSpace": { "sid": "WorldSensing.vhu6lom3sovk6ahpogebfewk5kqadvs4.5385fc250cf2497dfe5679d1" } }, { "ParkingSpace": { "sid": "WorldSensing.vhu6lom3sovk6ahpogebfewk5kqadvs4.5385ff2f0cf2497dfe567c0c" } }, { "ParkingSpace": { "sid": "WorldSensing.vhu6lom3sovk6ahpogebfewk5kqadvs4.5385fd700cf2e65ecf6330c6" } }, { "ParkingSpace": { "sid": "WorldSensing.vhu6lom3sovk6ahpogebfewk5kqadvs4.5385fefe0cf2497dfe567bee" } }, { "ParkingSpace": { "sid": "WorldSensing.vhu6lom3sovk6ahpogebfewk5kqadvs4.5385ff690cf2497dfe567c3f" } }, { "ParkingSpace": { "sid": "WorldSensing.vhu6lom3sovk6ahpogebfewk5kqadvs4.55e972d21170d0c2fd7d15b1" } }]
我想尝试如下:
private String generateParkingspaceBody(final ListlistOfsIds) { //sids array JSONArray sidsArray = new JSONArray(); for (String sId: listOfsIds) { //creating sidObject and object JSONObject sIdObject = new JSONObject(); JSONObject object = new JSONObject(); try { sIdObject.put("sid", sId); object.put("ParkingSpace",sIdObject); sidsArray.put(object); } catch (JSONException e) { CPALog.e(TAG,e.getMessage()); } } return sidsArray.toString(); }
将此字符串发送到服务调用中,如:
Response getNearByParkingSpaces(@Header("Authorization") String accessToken, @Header("Content-Type") String contentType, @Body String arrayOfSids);
但是在logact中显示的请求是:
"[{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}},{\"ParkingSpace\":{}}]"
请帮帮我,如何发送此请求?
提前致谢.
您无需将对象转换为JSONArray,Retrofit将自动为您执行此操作.
只需将您的API方法声明更改为:
@Headers({ "Content-type: application/json" }) Response getNearByParkingSpaces(@Header("Authorization") String accessToken, @Body ListarrayOfSids);