【问题标题】:put value inside Hashmap from another Method inside another Class将值从另一个类中的另一个方法放入 Hashmap
【发布时间】:2015-02-10 02:38:31
【问题描述】:

我正在尝试创建一个英雄联盟 APP。我有一个我无法解决的问题。我已经声明了一个 Hashmap 和 ArrayList。首先,我使用 JSON 从英雄联盟 API 解析中获取数据。然后我将数据放入Hashmap。但是,当我尝试将数据从另一个类中的另一个方法放入 Hashmap 时。这没用。 enter code hereif (response.isSuccessful()) { JSONObject jsonObj = new JSONObject(jsonData); JSONArray 参与者 = jsonObj.getJSONArray("participants"); mourTeamSummonerList = new ArrayList>(); menemyTeamSummonerList = new ArrayList>();

                    for(int i = 0; i< participants.length();i++){
                        JSONObject p = participants.getJSONObject(i);
                        long teamId = p.getLong("teamId");
                        HashMap<String, Object> participant = new HashMap<String, Object>();
                        if(teamId == 100){

                            long championId = p.getLong("championId");
                            String summonerName = p.getString("summonerName");
                            long summonerId = p.getLong("summonerId");
                            if(summonerName==IDName){
                                ifOurTeam = true;
                            }

                            participant.put("summonerName",summonerName);
                            methods.ChampionSquare(championId, participant);
                            participant.put("summonerId",summonerId);
                            methods.getSummonerInfoFromId(summonerName,participant);
                            System.out.println(participant);
                            mourTeamSummonerList.add(participant);
                        }
                        if(teamId == 200){

                            long championId = p.getLong("championId");
                            String summonerName = p.getString("summonerName");
                            long summonerId = p.getLong("summonerId");

                            participant.put("summonerName",summonerName);
                            methods.ChampionSquare(championId, participant);
                            participant.put("summonerId",summonerId);
                            methods.getSummonerInfoFromId(summonerName,participant);
                            System.out.println(participant);
                            menemyTeamSummonerList.add(participant);
                        }

public class Methods {
public void getSummonerInfoFromId(final String SumName, final HashMap<String, Object> participant) {

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/"+SumName.replace(" ","")+"?api_key=7c75d244-fad7-4d18-8038-02a75287fce2")
            .build();

    Call call = client.newCall(request);
    call.enqueue(new Callback() {


        @Override
        public void onFailure(Request request, IOException e) {
            getSummonerInfoFromId(SumName,participant);
        }

        @Override
        public void onResponse(Response response) throws IOException {


            try {
                String jsonData = response.body().string();



                    JSONObject jsonObj = new JSONObject(jsonData);
                    JSONObject game = jsonObj.getJSONObject(SumName.toLowerCase().replace(" ",""));

                    String summonerLevel = Long.toString(game.getLong("summonerLevel"));


                    participant.put("summonerLevel",summonerLevel);


            }
            catch (IOException e) {

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    });

}
        public void ChampionSquare(final long championId, final HashMap<String, Object> participant) {
    if(championId==1){
        participant.put("championId", R.drawable.annie);
    }
    else if(championId==2){
        participant.put("championId",R.drawable.olaf);
    }
    else if(championId==3){
        participant.put("championId",R.drawable.galio);
    }
    else if(championId==4){
        participant.put("championId",R.drawable.twistedfate);
    }
    else if(championId==5){
        participant.put("championId", R.drawable.xinzhao);
    }

当我打印出 Hashmap 时,它会打印出部分正确的键和值,用于 summererName、championId 和 summererId。我认为我做错的是将键和值放在内部类 Hashmap 中:

  • {summonerName=whoiam2, ChampionId=2130837580, summonerId=20647588} {summonerName=StillHere4U, ChampionId=2130837617, summonerId=43639591} {summonerName=StillThere4U, ChampionId=2130837564,summonerId=47639078} {summonerName=Kairu 仁谷,冠军ID=2130837584,召唤师ID=22276202} {summonerName=halmooncircle, ChampionId=2130837573, summonerId=31202000} {summonerName=NineWorlds, ChampionId=2130837576, summonerId=31544164} {summonerName=Masterzeus92, 冠军ID=2130837672,召唤师ID=34979540} {summonerName=Feeeeeeeeeeeeeesh, ChampionId=2130837577, summonerId=27273581} {summonerName=savedolphins, ChampionId=2130837664,summonerId=40901971} {summonerName=幸运 Th1rt33n,冠军ID=2130837660,召唤师ID=34587805}

【问题讨论】:

  • 你的问题是什么?
  • 当我打印出 Hashmap 时,它会打印出部分正确的键和值,用于 summererName、championId 和 summererId。缺少 SummonerLevel 键,是因为我将键放在内部类中吗?以及如何解决它
  • ID 有什么问题?
  • 它应该有 4 个 ID,summonerName、championId、summonerId 以及 summererLevel ID
  • 除了关卡丢失之外,ID和名称有什么不正确的地方?你说他们“部分不正确”。

标签: java android hashmap


【解决方案1】:

这个:

new Callback() {


    @Override
    public void onFailure(Request request, IOException e) {
        getSummonerInfoFromId(SumName,participant);
    }

    @Override
    public void onResponse(Response response) throws IOException {


        try {
            String jsonData = response.body().string();



                JSONObject jsonObj = new JSONObject(jsonData);
                JSONObject game = jsonObj.getJSONObject(SumName.toLowerCase().replace(" ",""));

                String summonerLevel = Long.toString(game.getLong("summonerLevel"));


                participant.put("summonerLevel",summonerLevel);


        }
        catch (IOException e) {

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}

不设置“summonerLevel”键。它创建了一个Callback当它的 onResponse 方法被调用时,它会设置“summonerLevel”键。然后你将Callback 传递给call.enqueue

根据名称,我假设call.enqueue 确实立即发出请求,因此它不会立即调用onResponse,因此“summonerLevel”键不会立即设置。

稍后,调用将完成,您的回调的 onResponse 方法将被调用,并且您将设置“summonerLevel”键 - 但这将在您已经很久之后发生打印出来。

看看是否有办法等待调用完成。然后,等待调用完成后再从getSummonerInfoFromId 返回。

【讨论】:

  • 那我相信我不应该异步请求这个
  • 天哪,这真的奏效了,我所要做的就是摆脱 call.enque
猜你喜欢
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 2013-11-23
  • 2012-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多