Java 在AsyncTask中通过其ID获取championName
所以我正在开发Android应用程序,它将显示最后10场比赛的召唤者细节。 我有冠军的钥匙,但我需要他的名字。在data dragon中,我找到了以下链接: 我想通过他的“钥匙”检索champName,因为我想下载champion图像。 我正在使用这个库: 但是我得到了这个错误NetworkOnMainThreadException,因为我不能在主线程中使用网络,但只有在onPostExecute中,我得到了champion的“密钥”,这是使用champion类获取getDataChampion(平台,密钥)所需的,那么如何解决这个问题呢?也许我可以发送“key”,然后使用champion.json:循环所有json文件,直到找到我的“key”并得到我的名字,但我不知道如何做到这一点 已编辑Java 在AsyncTask中通过其ID获取championName,java,android,api,android-asynctask,Java,Android,Api,Android Asynctask,所以我正在开发Android应用程序,它将显示最后10场比赛的召唤者细节。 我有冠军的钥匙,但我需要他的名字。在data dragon中,我找到了以下链接: 我想通过他的“钥匙”检索champName,因为我想下载champion图像。 我正在使用这个库: 但是我得到了这个错误NetworkOnMainThreadException,因为我不能在主线程中使用网络,但只有在onPostExecute中,我得到了champion的“密钥”,这是使用champion类获取getDataChampion
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"); }
}
}
类包装器
{
传唤者传唤者;
匹配列表;
里奥塔皮;
}
@SuppressLint(“StaticFieldLeak”)
公共类FetchMatchListTask扩展了AsyncTask{
@凌驾
受保护的包装器doInBackground(字符串…参数){
ApiConfig config=new ApiConfig().setKey(“MY-API-KEY”);
wrapper.api=新的RiotApi(配置);
冠军;
试一试{
//wrapper.champion=wrapper.api.getDataChampion(Platform.EUNE,match.getChampion());
wrapper.caller=wrapper.api.getcallerByName(Platform.EUNE,params[0]);
wrapper.matchList=wrapper.api.getMatchListByAccountId(Platform.EUNE,wrapper.caller.getAccountId());
if(wrapper.matchList.getMatches()!=null){
for(MatchReference匹配:wrapper.matchList.getMatches()){
如果(matchLimitCounter==10)中断;
否则{
匹配计数器++;
int key=match.getChampion();
champion=wrapper.api.getDataChampion(Platform.EUNE,key);
字符串champName=champion.getName();
//在这里,我将把我的championName发送到我自己的Match类,然后我将使用毕加索将img放入ImageView中
//和datadragon champion assest链接
Match myMatch=新匹配(wrapper.caller.getProfileIconId(),champName,key,“lala”);
匹配列表。添加(myMatch);
}
}
}
返回包装器;
}捕获(RiotapieException e){
Log.d(“RiotMSG”,“Blad:”+RiotApiException.getMessage(e.getErrorCode());
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(包装器wrapp){
super.onPostExecute(wrapp);
if(wrapp!=null){
numofagestext.setText(String.valueOf(wrapp.matchList.getTotalGames());
}
如果(wrapp==null){抛出新的NullPointerException(“wrapp.matchList对象为null”);}
}
}
}那些包装器操作是否与网络/IO相关
wrapp.champion = wrapper.api.getDataChampion(Platform.EUNE, key);
String champName = wrapp.champion.getName();
如果是,则将其移动到
doInBackground
中,而不是onPostExecute
。因为它们是繁重的任务,必须在主线程之外执行,即上述情况下的onPostExecute
[Look Edit]所以我把所有的都移到了doInBackground,但是现在我得到一个错误,wrapp=nullDebug从doInBackground内部开始,并检查是否实例化了wrapper
。问题是从库中返回403的getDataChampion(),与这里相同:,所以我看到获取championName的唯一方法是发送我的“密钥”并使用它和champion.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"); }
}
}
wrapp.champion = wrapper.api.getDataChampion(Platform.EUNE, key);
String champName = wrapp.champion.getName();