如何使用SoundCloud api包装器为java更新播放列表

时间:2013-02-07 08:34:09

标签: java api soundcloud

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

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

有什么想法吗?

1 个答案:

答案 0 :(得分: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;                                                                             
}                                                                                            

检查包装器中的测试以查看它们的运行情况: