如何从当前登录的用户那里获取 user_id?
How can I get the user_id from the user currently logged in?
我正在尝试从当前登录应用的用户那里获取私人播放列表。
SpotifyApi api = new SpotifyApi();
api.setAccessToken(response.getAccessToken());
SpotifyService spotify = api.getService();
Playlist playlist = spotify.getPlaylist(user_id, playlist_id);
如何获得user_id?
编辑
我试过这段代码:
@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);
}
这给了我一个错误:
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
我不确定您使用的是什么库,但它看起来像 spotify-web-api-android 包装器。
如果是这样,您可以通过调用 Get Current User's Profile endpoint using SpotifyService's getMe() method. getMe() will return a User object 来检索当前用户的用户 ID,该 Get Current User's Profile endpoint using SpotifyService's getMe() method. getMe() will return a User object 有一个名为 id 的成员。
更新:看来这个问题可能与包装器无关,而是一个普遍的 Android 问题。这 Stack Overflow question 似乎相关。
在进入 if 块之前添加检查以查看 resultCode 是否为 RESULT_CANCELED 可能会解决此问题。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_CANCELED && resultCode == REQUEST_CODE) {
AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, data);
...
}
}
其他人使用额外的 resultCode == RESULT_OK,我的理解也是有效的。
我必须在 AsyncTask 中获取用户对象,因为无法在主线程上执行网络操作。这同样适用于获取用户播放列表。
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
}
}
在主线程上:
new MyTask().execute("");
我正在尝试从当前登录应用的用户那里获取私人播放列表。
SpotifyApi api = new SpotifyApi();
api.setAccessToken(response.getAccessToken());
SpotifyService spotify = api.getService();
Playlist playlist = spotify.getPlaylist(user_id, playlist_id);
如何获得user_id?
编辑
我试过这段代码:
@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);
}
这给了我一个错误:
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
我不确定您使用的是什么库,但它看起来像 spotify-web-api-android 包装器。
如果是这样,您可以通过调用 Get Current User's Profile endpoint using SpotifyService's getMe() method. getMe() will return a User object 来检索当前用户的用户 ID,该 Get Current User's Profile endpoint using SpotifyService's getMe() method. getMe() will return a User object 有一个名为 id 的成员。
更新:看来这个问题可能与包装器无关,而是一个普遍的 Android 问题。这 Stack Overflow question 似乎相关。
在进入 if 块之前添加检查以查看 resultCode 是否为 RESULT_CANCELED 可能会解决此问题。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_CANCELED && resultCode == REQUEST_CODE) {
AuthenticationResponse response = AuthenticationClient.getResponse(resultCode, data);
...
}
}
其他人使用额外的 resultCode == RESULT_OK,我的理解也是有效的。
我必须在 AsyncTask 中获取用户对象,因为无法在主线程上执行网络操作。这同样适用于获取用户播放列表。
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
}
}
在主线程上:
new MyTask().execute("");