Java 代码“;跳绳;JSON对象请求中的响应侦听器(截取)
我目前正在开发一个应用程序,我想阅读Spotify播放列表中给定ID的所有歌曲。我使用的是Volley、Gson和Spotify android认证。我已经成功地从Web API获得了所有用户播放列表的列表,但是在添加了歌曲阅读类之后,当我运行应用程序时,返回的歌曲数组是空的,并且在调试面板中,程序似乎跳过了responseListener(我还添加了多个未打印的日志语句以确保). 这是我的第一个android应用程序,我是Volley的新手,所以我感谢任何帮助 以下是歌曲阅读器代码:Java 代码“;跳绳;JSON对象请求中的响应侦听器(截取),java,android,json,android-volley,spotify,Java,Android,Json,Android Volley,Spotify,我目前正在开发一个应用程序,我想阅读Spotify播放列表中给定ID的所有歌曲。我使用的是Volley、Gson和Spotify android认证。我已经成功地从Web API获得了所有用户播放列表的列表,但是在添加了歌曲阅读类之后,当我运行应用程序时,返回的歌曲数组是空的,并且在调试面板中,程序似乎跳过了responseListener(我还添加了多个未打印的日志语句以确保). 这是我的第一个android应用程序,我是Volley的新手,所以我感谢任何帮助 以下是歌曲阅读器代码: publ
public class SongReaderSpotify {
private static final String ENDPOINT = "https://api.spotify.com/v1/playlists";
private SharedPreferences msharedPreferences;
private RequestQueue mqueue;
private ArrayList<Song> slist = new ArrayList<Song>();
private String playlistid;
private String templink;
public SongReaderSpotify(RequestQueue queue, SharedPreferences sharedPreferences, String id) {
mqueue = queue;
msharedPreferences = sharedPreferences;
playlistid = id;
templink = ENDPOINT +"/" +playlistid +"/tracks";
}
public ArrayList<Song> getSongs() {
return slist;
}
public ArrayList<Song> get(final VolleyCallBack callBack) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, templink, null, response -> {
Gson gson = new Gson();
Log.d("RESPONSE",response.toString());
JSONArray jsonArray = response.optJSONArray("items");
Log.d("RESPONSE",response.toString());
for (int n = 0; n < jsonArray.length(); n++) {
try {
JSONObject object = jsonArray.getJSONObject(n);
Log.d("RESPONSE OBJECT",object.toString());
JSONArray artistarr = object.optJSONArray("artists");
object = object.optJSONObject("track");
Song s = gson.fromJson(object.toString(), Song.class);
Log.d("SONG FIELDS",s.toString());
List<Artist> temp = new ArrayList<Artist>();
for(int i =0;i<artistarr.length();i++)
{
//GETTING Artists
try{
JSONObject o = artistarr.getJSONObject(i);
Artist a = gson.fromJson(o.toString(),Artist.class);
Log.d("ARTISTNAME",a.getName());
temp.add(a);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
s.setsArtists(temp);
slist.add(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
callBack.onSuccess();
}, error -> {
// TODO: Handle error
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
String token = msharedPreferences.getString("token", "");
String auth = "Bearer " + token;
headers.put("Authorization", auth);
return headers;
}
};
mqueue.add(jsonObjectRequest);
Log.d("SONGLIST", slist.toString());
return slist;
}
}
以下是检索用户播放列表的脚本供参考:
public class UserPlaylistsSpotify {
private static final String ENDPOINT = "https://api.spotify.com/v1/me/playlists";
private SharedPreferences msharedPreferences;
private RequestQueue mqueue;
private ArrayList<Playlist> plist = new ArrayList<Playlist>();
public UserPlaylistsSpotify(RequestQueue queue, SharedPreferences sharedPreferences) {
mqueue = queue;
msharedPreferences = sharedPreferences;
}
public ArrayList<Playlist> getPlaylists() {
return plist;
}
public ArrayList<Playlist> get(final VolleyCallBack callBack) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, ENDPOINT, null, response -> {
Gson gson = new Gson();
JSONArray jsonArray = response.optJSONArray("items");
for (int n = 0; n < jsonArray.length(); n++) {
try {
JSONObject imgobject = jsonArray.getJSONObject(n);
JSONObject plobject = jsonArray.getJSONObject(n);
Playlist p = gson.fromJson(plobject.toString(),Playlist.class);
JSONArray loopar = imgobject.optJSONArray("images");
List<Image> temp = new ArrayList<Image>();
for(int j = 0; j < loopar.length();j++)
{
//GETTING IMAGES
try{
JSONObject o = loopar.getJSONObject(j);
Image i = gson.fromJson(o.toString(),Image.class);
temp.add(i);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
p.addToImages(temp);
plist.add(p);
} catch (JSONException e) {
e.printStackTrace();
}
}
callBack.onSuccess();
}, error -> {
// TODO: Handle error
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
String token = msharedPreferences.getString("token", "");
String auth = "Bearer " + token;
headers.put("Authorization", auth);
return headers;
}
};
mqueue.add(jsonObjectRequest);
return plist;
}
}
我也是一个新的共享偏好,但经过一些调试后,似乎歌曲阅读器得到了播放列表阅读器相同的令牌
我一直在这个问题上有一段时间了,所以任何帮助都是非常感谢的!提前感谢:)
public class UserPlaylistsSpotify {
private static final String ENDPOINT = "https://api.spotify.com/v1/me/playlists";
private SharedPreferences msharedPreferences;
private RequestQueue mqueue;
private ArrayList<Playlist> plist = new ArrayList<Playlist>();
public UserPlaylistsSpotify(RequestQueue queue, SharedPreferences sharedPreferences) {
mqueue = queue;
msharedPreferences = sharedPreferences;
}
public ArrayList<Playlist> getPlaylists() {
return plist;
}
public ArrayList<Playlist> get(final VolleyCallBack callBack) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, ENDPOINT, null, response -> {
Gson gson = new Gson();
JSONArray jsonArray = response.optJSONArray("items");
for (int n = 0; n < jsonArray.length(); n++) {
try {
JSONObject imgobject = jsonArray.getJSONObject(n);
JSONObject plobject = jsonArray.getJSONObject(n);
Playlist p = gson.fromJson(plobject.toString(),Playlist.class);
JSONArray loopar = imgobject.optJSONArray("images");
List<Image> temp = new ArrayList<Image>();
for(int j = 0; j < loopar.length();j++)
{
//GETTING IMAGES
try{
JSONObject o = loopar.getJSONObject(j);
Image i = gson.fromJson(o.toString(),Image.class);
temp.add(i);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
p.addToImages(temp);
plist.add(p);
} catch (JSONException e) {
e.printStackTrace();
}
}
callBack.onSuccess();
}, error -> {
// TODO: Handle error
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
String token = msharedPreferences.getString("token", "");
String auth = "Bearer " + token;
headers.put("Authorization", auth);
return headers;
}
};
mqueue.add(jsonObjectRequest);
return plist;
}
}
2020-01-24 14:57:03.717 17922-17922/com.example.switcheroo D/STARTING: GOT AUTH TOKEN
2020-01-24 14:57:03.742 17922-17955/com.example.switcheroo D/EGL_emulation: eglMakeCurrent: 0xe7c85540: ver 2 0 (tinfo 0xe7c836f0)
2020-01-24 14:57:03.742 17922-18007/com.example.switcheroo W/cr_CrashFileManager: /data/user/0/com.example.switcheroo/cache/WebView/Crash Reports does not exist or is not a directory
2020-01-24 14:57:04.293 17922-17922/com.example.switcheroo W/mple.switchero: Accessing hidden field Lsun/misc/Unsafe;->theUnsafe:Lsun/misc/Unsafe; (light greylist, reflection)
2020-01-24 14:57:04.340 17922-17922/com.example.switcheroo D/ENDPOINT: https://api.spotify.com/v1/playlists/5tSCpEXtiHvlhSQneDTgXL/tracks
2020-01-24 14:57:04.341 17922-17922/com.example.switcheroo D/SONGLIST: []
2020-01-24 14:57:04.341 17922-17922/com.example.switcheroo D/ENDPOINT: https://api.spotify.com/v1/playlists/3RKyX5h0a24jHAK3PSMRcG/tracks
2020-01-24 14:57:04.342 17922-17922/com.example.switcheroo D/SONGLIST: []
2020-01-24 14:57:04.342 17922-17922/com.example.switcheroo D/ENDPOINT: https://api.spotify.com/v1/playlists/78awFOWllGbEH0Nskl7Rnh/tracks
2020-01-24 14:57:04.342 17922-17922/com.example.switcheroo D/SONGLIST: []
2020-01-24 14:57:04.342 17922-17922/com.example.switcheroo D/ENDPOINT: https://api.spotify.com/v1/playlists/37i9dQZF1DX6I0cdp2bBlZ/tracks
2020-01-24 14:57:04.342 17922-17922/com.example.switcheroo D/SONGLIST: []
2020-01-24 14:57:04.342 17922-17922/com.example.switcheroo D/ENDPOINT: https://api.spotify.com/v1/playlists/37i9dQZF1DX186v583rmzp/tracks
2020-01-24 14:57:04.343 17922-17922/com.example.switcheroo D/SONGLIST: []
2020-01-24 14:57:04.343 17922-17922/com.example.switcheroo D/ENDPOINT: https://api.spotify.com/v1/playlists/5AQkVd2L8aRfGheTDKYpLi/tracks
2020-01-24 14:57:04.343 17922-17922/com.example.switcheroo D/SONGLIST: []
2020-01-24 14:57:04.343 17922-17922/com.example.switcheroo D/ENDPOINT: https://api.spotify.com/v1/playlists/3Zu0udhojTUyjnFfnfYrMP/tracks
2020-01-24 14:57:04.343 17922-17922/com.example.switcheroo D/SONGLIST: []
2020-01-24 14:57:04.343 17922-17922/com.example.switcheroo D/ENDPOINT: https://api.spotify.com/v1/playlists/3NSc2VAy6kGPcaPzmONQlx/tracks
2020-01-24 14:57:04.344 17922-17922/com.example.switcheroo D/SONGLIST: []
2020-01-24 14:57:04.344 17922-17922/com.example.switcheroo D/ENDPOINT: https://api.spotify.com/v1/playlists/0Z89HetVHGGCYLCKx1EG7i/tracks
2020-01-24 14:57:04.344 17922-17922/com.example.switcheroo D/SONGLIST: []
2020-01-24 14:57:04.344 17922-17922/com.example.switcheroo D/ENDPOINT: https://api.spotify.com/v1/playlists/3ZkFvyGwLLSBHZc3aamjEl/tracks
2020-01-24 14:57:04.344 17922-17922/com.example.switcheroo D/SONGLIST: []
2020-01-24 14:57:04.344 17922-17922/com.example.switcheroo D/ENDPOINT: https://api.spotify.com/v1/playlists/37i9dQZF1DZ06evO2QRN3G/tracks
2020-01-24 14:57:04.345 17922-17922/com.example.switcheroo D/SONGLIST: []
2020-01-24 14:57:04.345 17922-17922/com.example.switcheroo D/ENDPOINT: https://api.spotify.com/v1/playlists/37i9dQZF1DZ06evO30Opqw/tracks
2020-01-24 14:57:04.345 17922-17922/com.example.switcheroo D/SONGLIST: []
2020-01-24 14:57:04.345 17922-17922/com.example.switcheroo D/ENDPOINT: https://api.spotify.com/v1/playlists/37i9dQZF1DZ06evO1BFaJa/tracks
2020-01-24 14:57:04.345 17922-17922/com.example.switcheroo D/SONGLIST: []
2020-01-24 14:57:04.345 17922-17922/com.example.switcheroo D/ENDPOINT: https://api.spotify.com/v1/playlists/37i9dQZF1DZ06evO1CSf4Y/tracks
2020-01-24 14:57:04.346 17922-17922/com.example.switcheroo D/SONGLIST: []
2020-01-24 14:57:04.348 17922-17922/com.example.switcheroo D/ENDPOINT: https://api.spotify.com/v1/playlists/3EMdqZxxfOIrlhQyiHaNfk/tracks
2020-01-24 14:57:04.348 17922-17922/com.example.switcheroo D/SONGLIST: []
2020-01-24 14:57:04.376 17922-17922/com.example.switcheroo W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@7956216
2020-01-24 14:57:04.600 17922-17922/com.example.switcheroo D/AndroidRuntime: Shutting down VM
2020-01-24 14:57:04.602 17922-17922/com.example.switcheroo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.switcheroo, PID: 17922