我正在尝试创建一个联盟传奇APP。我有一个我无法解决的问题。我已经声明了一个Hashmap和ArrayList。首先,我使用JSON从Legend of Legend API解析数据。然后我将数据输入Hashmap。但是,当我尝试从另一个类中的另一个方法将数据输入到Hashmap时。这是行不通的。
enter code here
if(response.isSuccessful()){
JSONObject jsonObj = new JSONObject(jsonData);
JSONArray参与者= jsonObj.getJSONArray("参与者");
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中:
答案 0 :(得分:0)
此:
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();
}
}
}
未设置&#34; summonerLevel&#34;键。它创建一个Callback
,当其onResponse方法被调用时,设置&#34; summonerLevel&#34;键。然后,您将Callback
传递给call.enqueue
。
根据名称,我认为call.enqueue
不立即发出请求,因此它不会立即调用onResponse
,并且所以&#34;召唤者等级&#34;密钥不会立即设置。
稍后,呼叫将完成,您的回叫的onResponse
方法将被调用,您将设置&#34;召唤级别&#34;密钥 - 但是在已经打印出来之后很久就会发生。
查看是否有办法等待通话完成。然后,等待呼叫完成后再从getSummonerInfoFromId
返回。