Specific Search Results Returning Empty (Spotify API)

barbecu :

I am currently working on an app involving the Spotify Web API, In the app i have a list of song objects, for each song i would like to search spotify and retrieve the corresponding URI for it, However certain songs return empty JSON responses... I know for sure that my requests are valid since I get responses for most of the songs, It's just a handful with this error. I also know that the songs exist on spotify and that they have the same exact name since the song objects were retrieved from spotify.

Here is an example of a request and an expected response I received:

Request: https://api.spotify.com/v1/search?q=Wu-Tang Clan Aint Nuthing ta F' Wit artist%3AWu-Tang Clan&type=track

Response:
{"tracks":{"href":"https:\/\/api.spotify.com\/v1\/search?query=Wu-Tang+Clan+Aint+Nuthing+ta+F%27+Wit+artist%3AWu-Tang+Clan&type=track&offset=0&limit=20","items":[{"album":{"album_type":"album","artists":[{"external_urls":{"spotify":"https:\/\/open.spotify.com\/artist\/34EP7KEpOjXcM2TCat1ISk"},"href":"https:\/\/api.spotify.com\/v1\/artists\/34EP7KEpOjXcM2TCat1ISk","id":"34EP7KEpOjXcM2TCat1ISk","name":"Wu-Tang Clan","type":"artist","uri":"spotify:artist:34EP7KEpOjXcM2TCat1ISk"}],"available_markets":
etc...

Here is an example of the error:

Request: https://api.spotify.com/v1/search?q=Da Mystery of Chessboxin' artist%3AWu-Tang Clan&type=track

Response:
{"tracks":{"href":"https:\/\/api.spotify.com\/v1\/search?query=Da+Mystery+of+Chessboxin%27+artist%3AWu-Tang+Clan&type=track&offset=0&limit=20","items":[],"limit":20,"next":null,"offset":0,"previous":null,"total":0}}

To make sure that the problem isn't on the API's end I also tried using the Try It Yourself feature in the API documentation and got the following results:

{
  "tracks": {
    "href": "https://api.spotify.com/v1/search?query=Da+Mystery+of+Chessboxin%27+artist%3AWu-Tang+Clan&type=track&offset=0&limit=20",
    "items": [
      {
        "album": {
          "album_type": "album",
          "artists": [
            {
              "external_urls": {
                "spotify": "https://open.spotify.com/artist/34EP7KEpOjXcM2TCat1ISk"
              },
              "href": "https://api.spotify.com/v1/artists/34EP7KEpOjXcM2TCat1ISk",
              "id": "34EP7KEpOjXcM2TCat1ISk",
              "name": "Wu-Tang Clan",
              "type": "artist",
              "uri": "spotify:artist:34EP7KEpOjXcM2TCat1ISk"
            }
etc...

As you can see the API received the same request both times, However from my app it returns an empty array of items. To make sure it didn't have anything to do with the amount of requests being sent in a small period of time, I requested this specific song only and still received an empty response from the API. If posting the code is necessary please tell me and i'll oblige, any help is appreciated :) !

EDIT: Here is the relevant code:

Sending the request:

 private SharedPreferences msharedPreferences;
    private RequestQueue mqueue;
    private String songuri;
    private String search_templink;

    public PosterUtilitySpotify(RequestQueue queue, SharedPreferences sharedPreferences, String templink) {
        mqueue = queue;         
        msharedPreferences = sharedPreferences;
        search_templink = templink;
    }
    public String getSongURI() {
        return songuri;
    }

    public String getResults(final VolleyCallBack callBack) {
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,search_templink, null, response -> {
            Gson gson = new Gson();
            JSONObject obj = response.optJSONObject("tracks");
            JSONArray jsonArray = obj.optJSONArray("items");
            try {
                JSONObject object = jsonArray.getJSONObject(0);
                URI u = gson.fromJson(object.toString(), URI.class);
                songuri =u.getUri();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            callBack.onSuccess();
        }, 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 songuri;
    }

constructing the request:

private static final String SEARCH_ENDPOINT = "https://api.spotify.com/v1/search?q=";

private ArrayList<String> getIDS(List<Song> slist){
        ArrayList<String> retlist = new ArrayList<String>();
        for(Song s : slist)
       {
           search_templink = SEARCH_ENDPOINT + s.getsName() +" artist%3A" + s.getArtist() +"&type=track";
     // both functions return a string, nothing special
            PosterUtilitySpotify psutil = new PosterUtilitySpotify(mqueue,msharedPreferences,search_templink);
            psutil.getResults(() ->{ retlist.add(psutil.getSongURI());
            });
       }
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return retlist;
    }
Bee :

The first problem is passing an non encoded url into JsonObjectRequest. It is easy to fix this by adapting the line setting search_templink to the following.

search_templink = SEARCH_ENDPOINT + URLEncoder.encode(s.getsName() +" artist:" + s.getArtist(), "UTF-8") + "&type=track";

There is another problem as we have figured out in the comment section. Having titles with special characters like " in the search query will cause the artist filter to break and therefore not return any results in the items array.

This isn't really fixable on client-side and can only be worked around. A pretty robust workaround is to only query for titles and then check the responded tracks for their artist until one matches the one specified. Keep in mind that paging might be needed in that case if there is no artist match on the first page of the response.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=417737&siteId=1