asynchronous - ANDROID: Async error "An error occured while executing doInBackground()" -
i'm using async tasks info api server , update ui. error @ line of 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 = 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 = 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"); = 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 = new bufferedinputstream(url2.openstream()); bd = bitmapfactory.decodestream(is); } catch(exception e2){} if(bd != null){ drawable[1] = new bitmapdrawable(bd); } return drawable; }
when debug it, url put in :
and website gives json response back.
i call method in doinbackground() method async task here:
drawable[] summonerspell = null; try { summonerspell = data.getsummonerspells(2); } catch (jsonexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }
and async task called 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 why error called? :)
from code in question, looks httpclient
not getting initialized.
if have declared (which seems do), can initialize before 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 = new httpget(url.tostring()); httpresponse r = httpclient.execute(get); //should work
also, note, make sure have internet
permission in androidmanifest.xml:
<uses-permission android:name="android.permission.internet" />
one more thing note, defaulthttpclient
has been deprecated.
see here guide using httpurlconnection
, replacement.
Comments
Post a Comment