将另一个方法中的值放入另一个 Class 中的 Hashmap
put value inside Hashmap from another Method inside another Class
我正在尝试制作英雄联盟APP。我有一个我无法解决的问题。我已经声明了一个 Hashmap 和 ArrayList。首先,我使用 JSON 从 League of Legend API 解析数据。然后我将数据放入 Hashmap。但是,当我尝试从另一个 Class 中的另一个方法将数据放入 Hashmap 时。这没用。
enter code here
if (response.isSuccessful()) {
JSONObject jsonObj = new JSONObject(jsonData);
JSON数组参与者=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 时,它打印出部分正确的 summonerName、championId 和 summonerId 键和值。我认为我做错的是将键和值放在内部类 Hashmap 中:
- {summonerName=whoiam2, championId=2130837580, summonerId=20647588}
{summonerName=StillHere4U, championId=2130837617,
summonerId=43639591} {summonerName=StillThere4U,
championId=2130837564, summonerId=47639078} {summonerName=凯鲁
Rengu, championId=2130837584, summonerId=22276202}
{summonerName=halmooncircle, championId=2130837573,
summonerId=31202000} {summonerName=NineWorlds, championId=2130837576,
summonerId=31544164} {summonerName=Masterzeus92,
championId=2130837672, summonerId=34979540}
{summonerName=Feeeeeeeeeeeeeeesh, championId=2130837577,
summonerId=27273581} {summonerName=savedolphins,
championId=2130837664, summonerId=40901971} {summonerName=Lucky
th1rt33n, championId=2130837660, summonerId=34587805}
这个:
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
.
返回之前等待调用完成
我正在尝试制作英雄联盟APP。我有一个我无法解决的问题。我已经声明了一个 Hashmap 和 ArrayList。首先,我使用 JSON 从 League of Legend API 解析数据。然后我将数据放入 Hashmap。但是,当我尝试从另一个 Class 中的另一个方法将数据放入 Hashmap 时。这没用。
enter code here
if (response.isSuccessful()) {
JSONObject jsonObj = new JSONObject(jsonData);
JSON数组参与者=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 时,它打印出部分正确的 summonerName、championId 和 summonerId 键和值。我认为我做错的是将键和值放在内部类 Hashmap 中:
- {summonerName=whoiam2, championId=2130837580, summonerId=20647588} {summonerName=StillHere4U, championId=2130837617, summonerId=43639591} {summonerName=StillThere4U, championId=2130837564, summonerId=47639078} {summonerName=凯鲁 Rengu, championId=2130837584, summonerId=22276202} {summonerName=halmooncircle, championId=2130837573, summonerId=31202000} {summonerName=NineWorlds, championId=2130837576, summonerId=31544164} {summonerName=Masterzeus92, championId=2130837672, summonerId=34979540} {summonerName=Feeeeeeeeeeeeeeesh, championId=2130837577, summonerId=27273581} {summonerName=savedolphins, championId=2130837664, summonerId=40901971} {summonerName=Lucky th1rt33n, championId=2130837660, summonerId=34587805}
这个:
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
.