【发布时间】: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和名称有什么不正确的地方?你说他们“部分不正确”。