返回空的特定搜索结果 (Spotify API)
Specific Search Results Returning Empty (Spotify API)
我目前正在开发一个涉及 Spotify Web 的应用程序 API,在该应用程序中我有一个歌曲对象列表,对于每首歌曲我想搜索 spotify 并为其检索相应的 URI,但是某些歌曲 return 空 JSON 响应...我确信我的请求是有效的,因为我得到了大多数歌曲的响应,只有少数有这个错误。我也知道这些歌曲存在于 spotify 上,并且它们具有相同的确切名称,因为歌曲对象是从 spotify 中检索到的。
这是我收到的请求和预期响应的示例:
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...
错误示例如下:
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}}
为确保问题不在 API 端,我还尝试使用 API 文档中的“自己尝试”功能并获得以下结果:
{
"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...
如您所见,API 两次收到相同的请求,但是在我的应用程序中,它 return 是一个空的项目数组。
为了确保它与短时间内发送的请求数量没有任何关系,我只请求了这首特定歌曲,但仍然收到 API 的空响应。如果需要发布代码,请告诉我,我会尽力而为,我们将不胜感激 :) !
编辑:
这是相关代码:
发送请求:
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;
}
构建请求:
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;
}
第一个问题是将未编码的 url 传递给 JsonObjectRequest
。通过将行设置 search_templink
调整为以下内容,很容易解决此问题。
search_templink = SEARCH_ENDPOINT + URLEncoder.encode(s.getsName() +" artist:" + s.getArtist(), "UTF-8") + "&type=track";
还有一个问题,我们已经在评论区找到了。在搜索查询中使用带有 "
等特殊字符的标题将导致 artist
过滤器中断,因此不会 return 在 items
数组中产生任何结果。
这在 client-side 上无法真正解决,只能解决。一个非常强大的解决方法是只查询标题,然后检查他们的艺术家的响应曲目,直到与指定的曲目相匹配。请记住,如果在响应的第一页上没有艺术家匹配,那么在这种情况下可能需要分页。
我目前正在开发一个涉及 Spotify Web 的应用程序 API,在该应用程序中我有一个歌曲对象列表,对于每首歌曲我想搜索 spotify 并为其检索相应的 URI,但是某些歌曲 return 空 JSON 响应...我确信我的请求是有效的,因为我得到了大多数歌曲的响应,只有少数有这个错误。我也知道这些歌曲存在于 spotify 上,并且它们具有相同的确切名称,因为歌曲对象是从 spotify 中检索到的。
这是我收到的请求和预期响应的示例:
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...
错误示例如下:
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}}
为确保问题不在 API 端,我还尝试使用 API 文档中的“自己尝试”功能并获得以下结果:
{
"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...
如您所见,API 两次收到相同的请求,但是在我的应用程序中,它 return 是一个空的项目数组。 为了确保它与短时间内发送的请求数量没有任何关系,我只请求了这首特定歌曲,但仍然收到 API 的空响应。如果需要发布代码,请告诉我,我会尽力而为,我们将不胜感激 :) !
编辑: 这是相关代码:
发送请求:
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;
}
构建请求:
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;
}
第一个问题是将未编码的 url 传递给 JsonObjectRequest
。通过将行设置 search_templink
调整为以下内容,很容易解决此问题。
search_templink = SEARCH_ENDPOINT + URLEncoder.encode(s.getsName() +" artist:" + s.getArtist(), "UTF-8") + "&type=track";
还有一个问题,我们已经在评论区找到了。在搜索查询中使用带有 "
等特殊字符的标题将导致 artist
过滤器中断,因此不会 return 在 items
数组中产生任何结果。
这在 client-side 上无法真正解决,只能解决。一个非常强大的解决方法是只查询标题,然后检查他们的艺术家的响应曲目,直到与指定的曲目相匹配。请记住,如果在响应的第一页上没有艺术家匹配,那么在这种情况下可能需要分页。