Apologies if this question is easily figured out, I'm quite new to Java and working with the Spotify API. I am using this Spotify Web API Java Package for a web application and am trying to implement a simplified Track Search method. I'm having trouble trying to figure out how to get the information I want after calling the method, which returns a Paging object. The Paging object is a container that holds a set of Track objects. The package I'm using gives a link to Paging<> objects in the Spotify API Documentation, but the Object Model Specifications page has apparently been removed and makes the link useless.
Here's the class I made (the client ID and secret have been omitted of course and the class does work with other methods that just return a float, int or String):
package trackour.trackour.views.api;
import java.io.IOException;
import org.apache.hc.core5.http.ParseException;
import com.neovisionaries.i18n.CountryCode;
import se.michaelthelin.spotify.SpotifyApi;
import se.michaelthelin.spotify.exceptions.SpotifyWebApiException;
import se.michaelthelin.spotify.model_objects.credentials.ClientCredentials;
import se.michaelthelin.spotify.model_objects.specification.Paging;
import se.michaelthelin.spotify.model_objects.specification.Track;
import se.michaelthelin.spotify.requests.authorization.client_credentials.ClientCredentialsRequest;
import se.michaelthelin.spotify.requests.data.search.simplified.SearchTracksRequest;
// Class to control the Spotify API and access its endpoints by using the Spotify Web API Java package
// Each method is public and static to easily use them throughout the application.
// ex. APIController.getAcousticness("ID GOES HERE");
public class APIController {
// create a SpotifyApi Builder object and set the Client ID and Client Secret
private static final SpotifyApi spotifyAPI = new SpotifyApi.Builder()
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
.build();
// create a ClientCredentialsRequest object which allows access to access tokens
private static final ClientCredentialsRequest clientCredentialsRequest = spotifyAPI.clientCredentials()
.build();
public static Paging<Track> searchTracks(String title) {
try {
final ClientCredentials clientCredentials = clientCredentialsRequest.execute();
spotifyAPI.setAccessToken(clientCredentials.getAccessToken());
final SearchTracksRequest searchTracksRequest = spotifyAPI.searchTracks(title)
.market(CountryCode.CA)
.limit(5)
.offset(0)
.build();
final Paging<Track> trackPaging = searchTracksRequest.execute();
return trackPaging;
} catch (IOException | SpotifyWebApiException | ParseException e) {
System.out.println(e.getMessage());
return null;
}
}
// TESTING
public static void main(String[] args) {
Paging<Track> test = searchTracks("Abba");
System.out.println(test.getTotal());
}
}
The code I have in the main function works and returns a Paging object which I can then get the total items the object has, but I can't get the information from each individual item. I've tried looking up the problem or a solution but I can't seem to find anything that would help me.
The Spotify Web API javadocs say that Paging<T>
has a method .getItems()
which in your case would return an array of Tracks
.
For example,
public static void main(String[] args) {
Paging<Track> test = searchTracks("Abba");
Track[] tracks = test.getItems();
System.out.println(Arrays.toString(tracks));
}
would print out all the tracks.