gpt4 book ai didi

java - 如何在 Android 中指定 Spotify Web API 的国家代码?

转载 作者:太空宇宙 更新时间:2023-11-04 06:06:56 24 4
gpt4 key购买 nike

在我的 Android 应用程序中,我试图获取艺术家的前十首轨道。我正在使用 wrapper用于 Spotify Web API。要获取艺术家的前十首轨道,您必须向 getArtistTopTrack(id) 方法提供艺术家的 id

这是我的代码,旨在做到这一点:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_ten);

Intent intent = getIntent();

String artistID = "";
artistID = intent.getExtras().getString("ID");

FetchTopTenTracksTask topTenTracksTask = new FetchTopTenTracksTask();
topTenTracksTask.execute(artistID);
}

private class FetchTopTenTracksTask extends AsyncTask<String, Void, Tracks> {

@Override
protected Tracks doInBackground(String... params) {
SpotifyApi api = new SpotifyApi();
SpotifyService spotify = api.getService();

return spotify.getArtistTopTrack(params[0]);
}

我收到的错误是

E/AndroidRuntime(2882): FATAL EXCEPTION: AsyncTask #2
E/AndroidRuntime(2882): java.lang.RuntimeException: An error occured while executing doInBackground()

这是由doInBackground引起的。我的想法是,错误的请求是由这行代码引起的,因为我需要为搜索指定一个国家/地区代码(API 需要这个),但我不知道该怎么做。

return spotify.getArtistTopTrack(params[0]);

获取艺术家热门轨道的正确端点是

GET https://api.spotify.com/v1/artists/{the artists id}/top-tracks?country={ISO 3166-1 alpha 2 country code}

但是当我检查 LogCat 以查看它使用的端点时,结果发现它是错误的

D/Retrofit(2882): <--- HTTP 400 https://api.spotify.com/v1/artists/{the artist id}%3Fcountry%3DIE/top-tracks (59ms)

如您所见,国家/地区代码“IE”被附加到艺术家 id 后面,如下所示:“%3Fcountry%3DIE”。这就是导致错误的原因,因为由于语法格式错误,服务器无法理解该请求。

有人可以告诉我如何将 "?country={country code}" 附加到端点,而不是将其放在艺术家 ID 之后吗?

最佳答案

为我自己的问题提供解决方案:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top_ten);

listView = (ListView) findViewById(R.id.listView);

Intent intent = getIntent();

String artistID = "";
artistID = intent.getExtras().getString("ID");

FetchTopTenTracksTask topTenTracksTask = new FetchTopTenTracksTask();
topTenTracksTask.execute(artistID);
}

private class FetchTopTenTracksTask extends AsyncTask<String, Void, Tracks> {

@Override
protected Tracks doInBackground(String... params) {

try {
String artistID = params[0];

Map<String, Object> options = new Hashtable<String, Object>();
options.put("country", "IE"); //Can be any country code here

SpotifyApi api = new SpotifyApi();
SpotifyService spotify = api.getService();

return spotify.getArtistTopTrack(artistID, options);
}
catch (RetrofitError retrofitError) {

Log.d("Retrofit Error: ", retrofitError.toString());
}
return null;
}

Here是我得到解决方案的地方。

关于java - 如何在 Android 中指定 Spotify Web API 的国家代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31432429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com