【发布时间】:2019-11-19 13:54:42
【问题描述】:
所以我正在开发 Android 应用程序,它将显示最近 10 场比赛以及 Summoner 的详细信息。 我有冠军的“钥匙”,但我需要他的名字。在数据龙中,我找到了这个链接: http://ddragon.leagueoflegends.com/cdn/9.22.1/img/champion/Aatrox.png 我想通过他的“密钥”以某种方式检索 champName,因为我想下载冠军图像。 我正在使用这个库:https://github.com/taycaldwell/riot-api-java
class Wrapper
{
Summoner summoner;
MatchList matchList;
RiotApi api;
//Champion champion;
}
@SuppressLint("StaticFieldLeak")
public class FetchMatchListTask extends AsyncTask<String, Void, Wrapper> {
@Override
protected Wrapper doInBackground(String... params) {
ApiConfig config = new ApiConfig().setKey("MY-API-KEY");
wrapper.api = new RiotApi(config);
try {
//wrapper.champion = wrapper.api.getDataChampion(Platform.EUNE, match.getChampion());
wrapper.summoner = wrapper.api.getSummonerByName(Platform.EUNE, params[0]);
wrapper.matchList = wrapper.api.getMatchListByAccountId(Platform.EUNE, wrapper.summoner.getAccountId());
return wrapper;
} catch (RiotApiException e) {
Log.d("RiotMSG", "Blad: "+ RiotApiException.getMessage(e.getErrorCode()));
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Wrapper wrapp) {
super.onPostExecute(wrapp);
if(wrapp != null) {
numOfGamesText.setText(String.valueOf(wrapp.matchList.getTotalGames()));
}
if (wrapp == null) { throw new NullPointerException("wrapp.matchList object is null"); }
if (wrapp.matchList.getMatches() != null) {
for (MatchReference match : wrapp.matchList.getMatches()) {
if(matchLimitCounter == 10) break;
else{
matchLimitCounter++;
int key = match.getChampion();
//wrapp.champion = wrapper.api.getDataChampion(Platform.EUNE, key);
//String champName = wrapp.champion.getName();
//Here i will send my championName to my own Match class and then i will
//put img in ImageView using picasso and datadragon champion assest link
Match match1 = new Match(wrapp.summoner.getProfileIconId(), match.getLane(), key, "lala");
matchList.add(match1);
}
}
}
}
}
我知道我可以在 onPostExecute 中做这样的事情,它会给我冠军名称:
wrapp.champion = wrapper.api.getDataChampion(Platform.EUNE, key);
String champName = wrapp.champion.getName();
但是我得到这个错误 NetworkOnMainThreadException 因为我不能在主线程中使用网络,但只有在 onPostExecute 我得到了我的冠军“密钥”,这是使用冠军类 getDataChampion(Platform, key) 所需要的,那么如何解决这个问题?也许我可以只发送“密钥”,然后使用 Champion.json:http://ddragon.leagueoflegends.com/cdn/9.22.1/data/en_US/champion.json 循环所有 json 文件,直到找到我的“密钥”并得到我的名字,但我不知道该怎么做。
已编辑
class Wrapper
{
Summoner summoner;
MatchList matchList;
RiotApi api;
}
@SuppressLint("StaticFieldLeak")
public class FetchMatchListTask extends AsyncTask<String, Void, Wrapper> {
@Override
protected Wrapper doInBackground(String... params) {
ApiConfig config = new ApiConfig().setKey("MY-API-KEY");
wrapper.api = new RiotApi(config);
Champion champion;
try {
//wrapper.champion = wrapper.api.getDataChampion(Platform.EUNE, match.getChampion());
wrapper.summoner = wrapper.api.getSummonerByName(Platform.EUNE, params[0]);
wrapper.matchList = wrapper.api.getMatchListByAccountId(Platform.EUNE, wrapper.summoner.getAccountId());
if (wrapper.matchList.getMatches() != null) {
for (MatchReference match : wrapper.matchList.getMatches()) {
if(matchLimitCounter == 10) break;
else{
matchLimitCounter++;
int key = match.getChampion();
champion = wrapper.api.getDataChampion(Platform.EUNE, key);
String champName = champion.getName();
//Here i will send my championName to my own Match class and then i will put img in ImageView using picasso
//and datadragon champion assest link
Match myMatch = new Match(wrapper.summoner.getProfileIconId(), champName, key, "lala");
matchList.add(myMatch);
}
}
}
return wrapper;
} catch (RiotApiException e) {
Log.d("RiotMSG", "Blad: "+ RiotApiException.getMessage(e.getErrorCode()));
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Wrapper wrapp) {
super.onPostExecute(wrapp);
if(wrapp != null) {
numOfGamesText.setText(String.valueOf(wrapp.matchList.getTotalGames()));
}
if (wrapp == null) { throw new NullPointerException("wrapp.matchList object is null"); }
}
}
}
【问题讨论】:
标签: java android api android-asynctask