Search code examples
androidspotifyplaylist

Retrieve my Spotify playlist id from my Android app


I'm trying to retrieve the id of my playlists in Spotify so that I can show the content (playlists name, tracks, etc.) in a ListView in my Android app. I can't find a way to get that data.

I copied the Android Quick Start source code given by Spotify. Everything works clean as long as I know the playlist ID ("spotify:playlist:5smL9JMG88uvAabko1c5P0"). But what about if I don't know it and I want to get it from Spotify (so, for example, if I have many playlists in my account I can decide in my app which one to play)? Is there a get method for Android? I don't show any code because it's the same provided by Spotify.


Solution

  • If you do not know the playlist id of the playlist you can use the following Spotify Web API Endpoint to get all of the users playlists : Get List Users Playlists

    There are libraries for the Spotify Web API, therefore you do not need to implement this by yourself if you do not want to.

    If you choose the kaaes/spotify-web-api-android library, the code could be something like this:

    SpotifyApi api = new SpotifyApi();
    api.setAccessToken("yourAccessToken");
    SpotifyService spotify = api.getService();
    
    spotify.getMyPlaylists(new SpotifyCallback<Pager<PlaylistSimple>>() {
        @Override
        public void failure(SpotifyError spotifyError) {
            // handle error
        }
    
        @Override
        public void success(Pager<PlaylistSimple> playlistSimplePager, Response response) {
            // do something
            // for example
            for(PlaylistSimple playlistSimple: playlistSimplePager.items) {
                Log.d("playlist:", playlistSimple.name);
            }
        }
    });