等待2个异构施法



您好,我正在研究一个LOL Appliac,我在异步任务并从2个不同的JSON来源获取数据有问题。我需要做的是从1个JSON源获取一些信息(获取英雄ID),然后将该信息提供给另一个任务以获取英雄的名称(另一个JSON源),然后将两个源中的信息从两个源保存到arraylaylist中。我不知道如何使这两个async任务互相等待,然后写下数据。在我设法使用静态字段和从1个任务中保存数据,然后将其保存到第二个问题之前好吧...现在我使用了loopj库,但我仍然无法将异步任务结合起来。而且我不能为您提供LOL API密钥,因为它应该是私人的,但我希望有人能够帮助我。预先感谢。

    package com.example.lolscouterv2;
import java.io.Console;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.loopj.android.http.*;
public class SummonerGames extends ListActivity {
    String summonerNameId, summonerRegion;
    String finalURL1 = "https://prod.api.pvp.net/api/lol/";
    String finalURL3 =  "/v1.3/game/by-summoner/";
    String finalURL2 = "/recent?api_key=";
    String finalURL;
    JSONArray jsonArray, jArr;
    HashMap <String, String> map;
    JSONObject c1, c2, jArr1, c3;
    String champID = null;
    String champURL1="https://prod.api.pvp.net/api/lol/static-data/eune/v1/champion/";
    String champURL2 = "?api_key=";
    String urlChampion;
    ArrayList<HashMap<String, String>> summonerGames, summonerChamp;
    static String heroID = null;
    static boolean completed = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.summonergames);
        summonerNameId = getIntent().getStringExtra("summonerName");
        summonerRegion = getIntent().getStringExtra("summonerRegion");
        finalURL = finalURL1 + summonerRegion  + finalURL3 + summonerNameId + finalURL2;
        Toast.makeText(SummonerGames.this, finalURL, Toast.LENGTH_SHORT).show();
        summonerGames = new ArrayList<HashMap<String,String>>();
        final AsyncHttpClient client = new AsyncHttpClient();
        client.get(finalURL, new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(JSONObject response) {
                String kills = null, numOfDeaths = null, assists = null, minions = null;
                try {
                    jsonArray = response.getJSONArray("games");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                for (int i = 0; i < jsonArray.length(); i++){
                    map = new HashMap<String, String>();
                    try {
                        c1 = jsonArray.getJSONObject(i);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    try {
                        jArr = c1.getJSONArray("fellowPlayers");
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }

                    /*for (int j = 0; j < jArr.length(); j++){
                        String testSummonerId = null;
                    try {
                        jArr1 = jArr.getJSONObject(j);
                        //testSummonerId = jArr.getString(2);
                        Log.i("TEST", jArr1.toString());
                    } catch (JSONException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    try {
                        testSummonerId = jArr1.getString("summonerId");
                        Log.i("TEST222", testSummonerId);
                    } catch (JSONException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    Log.i("ERROR", testSummonerId);
                    Log.i("ERROR2", summonerNameId);
                    if(Integer.parseInt(summonerNameId)== Integer.parseInt(testSummonerId)){
                        try {
                            champID = jArr1.getString("championId");
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    map.put("heroid", champID);
                    }*/


                    try {
                        c2 = c1.getJSONObject("stats");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    try {
                        heroID = c1.getString("championId");
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                    String level = null;
                    urlChampion = champURL1 + heroID + champURL2;
                    try {
                        level = c2.getString("level");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    if (c2.isNull("championsKilled")){
                        kills = "0";
                    }
                    else{
                    try {
                        kills = c2.getString("championsKilled");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    }
                    if (c2.isNull("numDeaths"))
                        numOfDeaths = "0";
                    else
                        try {
                            numOfDeaths = c2.getString("numDeaths");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    if (c2.isNull("assists"))
                        assists = "0";
                    else
                        try {
                            assists = c2.getString("assists");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    if (c2.isNull("minionsKilled"))
                        minions = "0";
                    else
                        try {
                            minions = c2.getString("minionsKilled");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    String neutralMinions = null;
                    if (c2.isNull("neutralMinionsKilled")){
                    neutralMinions = "0";
                    }
                    else{
                         try {
                            neutralMinions = c2.getString("neutralMinionsKilled");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    int totalMinions = Integer.parseInt(minions)+ Integer.parseInt(neutralMinions);
                    String gold = null;
                    try {
                        gold = c2.getString("goldEarned");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    float gold1 = Float.parseFloat(gold)/1000; 
                    boolean gameEnd = false;
                    try {
                        gameEnd = c2.getBoolean("win");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    String gameResult;
                    if(gameEnd)
                        gameResult = "win";
                    else
                        gameResult = "lose";
                    int x = 0;
                    String data = getChamp(urlChampion);
                    while (completed){
                        x = x + 1;
                        System.out.print(x);
                    }
                    x = 0;
                    map.put ("kills", kills);
                    map.put ("assists", assists);
                    map.put ("deaths", numOfDeaths);
                    map.put ("level", "Level:" + level);
                    map.put("goldEarned", "Gold:" + String.valueOf(gold1) +"k");
                    map.put("gameResult", gameResult);
                    map.put("minions", "Minions:" + String.valueOf(totalMinions));
                    map.put("heroid", data);
                    summonerGames.add(map);
                    completed = true;
                }
                Log.i("test", jsonArray.toString());
                ListAdapter adapter = new SimpleAdapter(SummonerGames.this, summonerGames, R.layout.list_items,
                        new String[] { "kills", "assists",
                        "deaths", "goldEarned", "minions", "gameResult", "level", "heroid" }, new int[] { R.id.kills,
                        R.id.assists, R.id.deaths, R.id.gold, R.id.minions, R.id.gameStatus, R.id.heroLevel, R.id.heroName });
                setListAdapter(adapter);
            }
        });
    }
    static public String getChamp(String url){
        AsyncHttpClient client1 = new AsyncHttpClient();

        client1.get(url, new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(JSONObject response1){
                try {
                    heroID = response1.getString("name");
                    Log.i("TESTNAME", heroID);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                completed = false;
            }
        });



        return heroID;
    }
}

如果您使用的是loopj,那么呼叫已经是异步的,因此您无需自己做。您不能从 getChamp()方法返回 heroid ,因为它是在内部类中初始化的, new JSONHTTTTPRESPONSESPONSEHANDLER(){} 在您的情况下。我的猜测是,您的异步电话确实在等待,但是根据我以前的解释,您根本不会从第二个结果获得结果。我建议您查看回调界面,以查看如何使用它们。我要做的是创建一个回调界面,然后在 onsuccess 中使用它使用它,将其传递给主类。这是一个粗略的主意:

public MyClass implements GetHeroListener{
private GetHeroListener getHeroListener;
public interface GetHeroListener {
    public void getHeroCallback(String heroName);
}
@Override
public void getHeroCallback(String heroName) {
    //here you now get your data from the onSuccess and can do whatever you want example:
    makeNewHTTPCallToGetHeroDetails(myUrl, heroName);
}
static public String getChamp(String url){
    getHeroListener = (GetHeroListener) this; //this is the part where you "connect" your main class to your listner
    AsyncHttpClient client1 = new AsyncHttpClient();
    client1.get(url, new JsonHttpResponseHandler(){
        @Override
        public void onSuccess(JSONObject response1){
            try {
                heroID = response1.getString("name");
                getHeroListener.getHeroCallback(heroID); //this is the part where you call the interfaces listener method, it will call back to the class that implements it, just make sure you override the method 
                Log.i("TESTNAME", heroID);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            completed = false;
        }
    });
}

如果您仅在蜂窝上运行应用程序,请注意,默认情况下,Asynctask在单个线程上运行,因此您可以创建并执行3个asynctasks。每个人都会追随另一个。

如果您希望支持旧版本的Android版本,则可以自定义当前的asynctask线程池,也可以复制asynctask的代码:

http://grepcode.com/file/repository.grepcode.com/java/ext/ext/com.google.android/android/4.4.2_r1/android/asdroid/os/ashynctask.java?av=f=f=f=f

最新更新