【问题标题】:Get championName by his ID in AsyncTask通过 AsyncTask 中的 ID 获取 ChampionName
【发布时间】: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


    【解决方案1】:

    这些包装器操作是否与网络/IO 相关?

    wrapp.champion = wrapper.api.getDataChampion(Platform.EUNE, key);
    String champName = wrapp.champion.getName();
    

    如果是,请将它们移入 doInBackground 而不是 onPostExecute。因为它们是繁重的操作,必须在主线程之外执行,即上述情况下的onPostExecute

    【讨论】:

    • [查看编辑] 所以我把所有东西都移到了 doInBackground 但现在我收到了 wrapp = null 的错误
    • 从 doInBackground 开始调试并检查 wrapper 是否已实例化
    • 问题是 getDataChampion() 从库返回 403,与此处相同:github.com/taycaldwell/riot-api-java/issues/148,所以我看到获得冠军名称的唯一方法是发送我的“密钥”并使用它和 Champion.json 文件我想从那里得到名字
    • 感谢您的宝贵时间和帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多