Plan
Premium
Country
USA
Device
Samsung Galaxy S9
Operating System
Android
My Question or Issue
I'm totally unable to use the web API to remove a song from a playlist. Pretty sure I have the request set up correctly, I've checked everything multiple times.
It returns a 400 every time without any other details. Can someone please let me know what I may be doing wrong? I've tried it both with and without the quotes in the JSON object keys and values.
Thanks in advance! Here's the code:
private void removeCurrentlyPlayingSongFromPlaylist(String playlistId) {
JSONObject tracksToDelete = new JSONObject();
JSONArray tracksArray = new JSONArray();
JSONObject thisTrackObject = new JSONObject();
try {
thisTrackObject.put("\"uri\"", "\"" + currentTrack.uri + "\"");
tracksArray.put(thisTrackObject);
tracksToDelete.put("\"tracks\"", tracksArray);
String requestUrl = getPlaylistTracksAPICallUrl(playlistId);
volleyQueue.makeRequest(requestUrl, VolleyQueue.REQUEST_TYPE_DELETE, response -> {
Toast.makeText(MainActivity.this, "Song successfully removed from playlist!", Toast.LENGTH_SHORT).show();
}, error -> {
int z = 0;
}, tracksToDelete);
} catch (JSONException e) {
}
}
public void makeRequest(String requestUrl, int requestType, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener, JSONObject jsonData) {
JsonObjectRequest request = null;
switch (requestType) {
case REQUEST_TYPE_GET:
request = getVolleyGETRequest(requestUrl, listener, errorListener);
break;
case REQUEST_TYPE_DELETE:
request = getVolleyDELETERequest(requestUrl, listener, errorListener, jsonData);
break;
case REQUEST_TYPE_POST:
request = getVolleyPOSTRequest(requestUrl, listener, errorListener);
break;
}
if (request != null) {
queue.add(request);
}
}
private JsonObjectRequest getVolleyDELETERequest(String url, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener, JSONObject jsonData) {
return new JsonObjectRequest(Request.Method.DELETE, url, jsonData, listener, errorListener) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Authorization", "Bearer " + preferences.getString(StandardStrings.PreferencesKeys.SPOTIFY_ACCESS_TOKEN, "null"));
params.put("Content-Type", "application/json");
return params;
}
};
}