0

Aim: Create a playlist called MonthlyTest after a onClick()

Code:

    public void createPlaylist(){
    String CREATE_PLAYLIST = "https://api.spotify.com/v1/users/"+username+"/playlists";
    JsonObjectRequest jsonRequest = new JsonObjectRequest
            (Request.Method.POST, CREATE_PLAYLIST, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        MONTHLY_PLAYLIST_ID = response.getJSONArray("items").getJSONObject(0).getString("uri");
                        MONTHLY_PLAYLIST_NAME = response.getJSONArray("items").getJSONObject(0).getString("name");
                        Log.i("MainActivity", response.toString(4));
                    } catch (JSONException e) {
                        Log.e("MainActivity", "Error whilst parsing JSONObject");
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("MainActivity", "Error whilst creating playlist");
                    error.printStackTrace();

                }
            }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Authorization", "Bearer " + token);
            return headers;
        }

        @Override
        public Map<String, String> getParams() throws AuthFailureError {
            HashMap<String, String> params = new HashMap<>();
            params.put("name", MONTHLY_PLAYLIST);
            params.put("description", "Monthly playlist with my current favourite songs.");
            params.put("public", "true");
            return params;
        }
    };
    requestQueue.add(jsonRequest);
}

Error:

06-05 12:04:49.666 7826-7846/applicationname.companydomain.simpleapp E/Volley: [275] BasicNetwork.performRequest: Unexpected response code 400 for https://api.spotify.com/v1/users/iwishiwasaneagle/playlists
06-05 12:04:49.666 7826-7826/applicationname.companydomain.simpleapp E/MainActivity: Error whilst creating playlist
06-05 12:04:49.666 7826-7826/applicationname.companydomain.simpleapp W/System.err: com.android.volley.ClientError
06-05 12:04:49.666 7826-7826/applicationname.companydomain.simpleapp W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:190)
06-05 12:04:49.666 7826-7826/applicationname.companydomain.simpleapp W/System.err:     at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120)
06-05 12:04:49.666 7826-7826/applicationname.companydomain.simpleapp W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)

Using https://developer.spotify.com/documentation/web-api/reference/playlists/create-playlist/ as a guideline, I'm almost certain that I have included all data but I can't find a resource to show me what I'm doing wrong. I have successfully implemented a few GETs from the SpotifyWebAPI.

1 Answers1

0

The problem was fixed using this: Android Volley: POST request - req.body inside of NodeJS REST API empty

getBody() has to be overriden, not getParams() as it actually never gets called by requests.