1

I'm using async tasks to get info from an api server and update the UI. However I get an error at this line of the code:

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;
}

When I debug it, the URL that is put in is this :

https://global.api.pvp.net/api/lol/static-data/na/v1.2/summoner-spell/12?api_key=9ed10e48-7ac1-422e-b3d0-fd75dedcc3b2

and this website gives an JSON response back.

I call this method in my doInBackground() method for the async task here:

        Drawable[] summonerSpell = null;
        try {
            summonerSpell = data.getSummonerSpells(2);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

And the async task is called from my first async task here:

    protected void onPostExecute(Long aLong) {
        super.onPostExecute(aLong);
        new BackgroundStuffGameOne().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, dataClass);
        new BackgroundStuffGameTwo().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, dataClass);
    }

Any ideas as to why this error is called? Thanks :)

Ryan Folz
  • 43
  • 5

1 Answers1

0

From the code in the question, it looks like httpClient is not getting initialized.

If you already have it declared (which it seems that you do), you can just initialize it before you use it:

 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

Also, just to note, make sure that you have the INTERNET permission in your AndroidManifest.xml:

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

One more thing to note, DefaultHttpClient has been deprecated.

See here for a good guide to using HTTPUrlConnection, which is a good replacement.

Community
  • 1
  • 1
Daniel Nugent
  • 40,780
  • 13
  • 103
  • 126