Android 安卓:异步错误“;执行doInBackground()时出错;

Android 安卓:异步错误“;执行doInBackground()时出错;,android,asynchronous,Android,Asynchronous,我使用异步任务从api服务器获取信息并更新UI。但是,我在这一行代码中得到一个错误: public Drawable[] getSummonerSpells(int game) throws JSONException, IOException { Drawable drawable[] = new Drawable[] {null, null}; Bitmap bd = null; int spell1 = 0, spell2 = 0; if(jsonSummo

我使用异步任务从api服务器获取信息并更新UI。但是,我在这一行代码中得到一个错误:

public Drawable[] getSummonerSpells(int game) throws JSONException, IOException {
    Drawable drawable[] = new Drawable[] {null, null};
    Bitmap bd = null;
    int spell1 = 0, spell2 = 0;
    if(jsonSummonerRecentGames!=null){
        JSONArray array = jsonSummonerRecentGames.getJSONArray("games");
        if((game) <= array.length()) {
            JSONObject object = array.getJSONObject(game - 1);
            if(object.has("spell1")) {
                spell1 = object.getInt("spell1");
            }
            if(object.has("spell2")){
                spell2 = object.getInt("spell2");
            }
        }
    }
    StringBuilder url = new StringBuilder("https://global.api.pvp.net/api/lol/static-data/" + region + "/v1.2/summoner-spell/" + spell1 + "?api_key=9ed10e48-7ac1-422e-b3d0-fd75dedcc3b2");
    HttpGet get = new HttpGet(url.toString());
    HttpResponse r = httpClient.execute(get); *********ERROR HERE*********
    HttpEntity e = r.getEntity();
    String data = EntityUtils.toString(e);
    JSONObject jsonObject = new JSONObject(data);
    String key = jsonObject.getString("key");
    try        {
        URL url2 = new URL("http://ddragon.leagueoflegends.com/cdn/5.2.1/img/spell/" + key + ".png");
        InputStream is = new BufferedInputStream(url2.openStream());
        bd = BitmapFactory.decodeStream(is);
    } catch(Exception e2){}
    if(bd != null){
        drawable[0] = new BitmapDrawable(bd);
    }
    url = new StringBuilder("https://global.api.pvp.net/api/lol/static-data/" + region + "/v1.2/summoner-spell/" + spell2 + "?api_key=9ed10e48-7ac1-422e-b3d0-fd75dedcc3b2");
    get = new HttpGet(url.toString());
    r = httpClient.execute(get);
    e = r.getEntity();
    data = EntityUtils.toString(e);
    jsonObject = new JSONObject(data);
    key = jsonObject.getString("key");
    try        {
        URL url2 = new URL("http://ddragon.leagueoflegends.com/cdn/5.2.1/img/spell/" + key + ".png");
        InputStream is = new BufferedInputStream(url2.openStream());
        bd = BitmapFactory.decodeStream(is);
    } catch(Exception e2){}
    if(bd != null){
        drawable[1] = new BitmapDrawable(bd);
    }
    return drawable;
}
异步任务是从我的第一个异步任务调用的:

        Drawable[] summonerSpell = null;
        try {
            summonerSpell = data.getSummonerSpells(2);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    protected void onPostExecute(Long aLong) {
        super.onPostExecute(aLong);
        new BackgroundStuffGameOne().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, dataClass);
        new BackgroundStuffGameTwo().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, dataClass);
    }

你知道为什么叫这个错误吗?谢谢:)

从问题中的代码来看,
httpClient
似乎没有初始化

如果您已经声明了它(看起来是这样),您可以在使用它之前初始化它:

 httpClient = new DefaultHttpClient(); //Added
 StringBuilder url = new StringBuilder("https://global.api.pvp.net/api/lol/static-data/" + region + "/v1.2/summoner-spell/" + spell1 + "?api_key=9ed10e48-7ac1-422e-b3d0-fd75dedcc3b2");
 HttpGet get = new HttpGet(url.toString());
 HttpResponse r = httpClient.execute(get); //Should work now
另外,需要注意的是,请确保您在AndroidManifest.xml中拥有
INTERNET
权限:

<uses-permission android:name="android.permission.INTERNET" />

还有一件事需要注意,
DefaultHttpClient


有关使用
HTTPUrlConnection
的良好指南,请参阅。这是一个很好的替代品。

在哪里初始化
httpClient