问题是您混合使用了 Rails 样式的表单参数和 JSON。
有两种选择:
1)只使用表单参数:
HttpResponse resp = api.put(Request.to("/playlists/123")
.with("playlist[title]", "updated title")
.with("playlist[tracks][][id]", 10001)
.with("playlist[tracks][][id]", 10002));
2) 以 JSON 格式提交播放列表数据:
private void updatePlaylist() {
JSONObject json = createJSONPlaylist("updated title", 10001, 10002);
HttpResponse resp = api.put(Request.to("/playlists/123")
.withContent(json.toString(), "application/json"));
}
private JSONObject createJSONPlaylist(String title, long... trackIds) throws JSONException {
JSONObject playlist = new JSONObject();
playlist.put("title", title);
JSONObject json = new JSONObject();
json.put("playlist", playlist);
JSONArray tracks = new JSONArray();
playlist.put("tracks", tracks);
for (long id : trackIds) {
JSONObject track = new JSONObject();
track.put("id", id);
tracks.put(track);
}
return json;
}
查看包装器中的测试以查看它们的实际效果: