1

soundcloud 文档没有使用 java 包装器更新播放列表的示例。我尝试过这样的事情,但它没有更新曲目。并且没有返回错误消息。

HttpResponse resp = wrapper
  .put(Request.to("/me/playlists/123")
  .with("playlist[title]", "updated title", "playlist[tracks]", "[
    {id: 10001},
    {id: 10002}
  ]"));

有任何想法吗?

4

1 回答 1

3

问题是您混合使用了 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;                                                                             
}                                                                                            

查看包装器中的测试以查看它们的实际效果:

于 2013-02-13T15:26:09.653 回答