1

I'm trying to get a private Playlist from the user currently logged in into the app.

SpotifyApi api = new SpotifyApi();
api.setAccessToken(response.getAccessToken());
SpotifyService spotify = api.getService();
Playlist playlist = spotify.getPlaylist(user_id, playlist_id);

How can I get user_id?

EDIT

I tried this code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST_CODE) {
        AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, data);
        if (response.getType() == AuthenticationResponse.Type.TOKEN) {
            SpotifyApi api = new SpotifyApi();
            api.setAccessToken(response.getAccessToken());
            SpotifyService spotify = api.getService();
            User user = spotify.getMe();
            Log.d("TAG", user.id);

        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

This gives me an error:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=Intent { (has extras) }} to activity {de.test.spotifytest/de.test.spotifytest.activities.MainActivity}: retrofit.RetrofitError

               

Antict
  • 597
  • 5
  • 22

2 Answers2

1

I had to get the user object in an AsyncTask because it isn't possible to perform network actions on the main thread. The same applies to getting the users playlists.

private class MyTask extends AsyncTask<String, Integer, Pager<Playlist>>{

    @Override
    protected Pager<Playlist> doInBackground(String... params) {
        Pager<Playlist> playlists = spotify.getPlaylists(spotify.getMe().id);
        return playlists;
    }

    @Override
    protected void onPostExecute(Pager<Playlist> playlistPager) {
       //do something with the playlists
    }
}

On main thread:

new MyTask().execute("");
Antict
  • 597
  • 5
  • 22
  • Thank you for sharing your solution. BTW, you can also use AsyncTask.execute(new Runnable() { @Override public void run() { } }); – david72 Dec 28 '17 at 19:46
0

I'm not sure what library you're using, but it looks like the spotify-web-api-android wrapper.

If so, you can retrieve the current user's user ID by calling the Get Current User's Profile endpoint using SpotifyService's getMe() method. getMe() will return a User object that has a member called id.

Update: It seems the issue may not be related to the wrapper, but rather a general Android issue. This Stack Overflow question seems related.

Adding a check to see if the resultCode isn't RESULT_CANCELED before entering the if block may solve this.

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
  if (resultCode != RESULT_CANCELED && resultCode == REQUEST_CODE) {
    AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, data);
    ...
  }
}

Others use an additional resultCode == RESULT_OK, which to my understanding is valid as well.

Community
  • 1
  • 1
Michael Thelin
  • 4,710
  • 3
  • 23
  • 29
  • Yes, I'm using the spotify-web-api-android wrapper. When I try to show the user id, it's giving me a runtimeException. I edited my question and added more code. What did i wrong? – Antict Mar 10 '15 at 16:48