android - Error on ruOnUiThread inside asychtask -
i trying parse data server , compare data data in database... seems working fine... except getting same value comparing data change... i'll explain code...
in below code, there textviews tv3 , tv4... comparing data ie, shopid changed, shows value first array of json...
when tried debug, found the run() inside runonuithread calling after 2-3 loops... donno whats happening here... please somone ,e sort out issue...
edit: while debugging when skip each line not showing error... when try skip breakpoints using f9 shows error...
edit: 2: since code litle bit long, juz deleted som unwanted part...
private bitmap downloadbitmap(string url) { httpurlconnection urlconnection = null; try { url uri = new url(url); urlconnection = (httpurlconnection) uri.openconnection(); int statuscode = urlconnection.getresponsecode(); if (statuscode != httpstatus.sc_ok) { return null; } inputstream inputstream = urlconnection.getinputstream(); if (inputstream != null) { bitmap = bitmapfactory.decodestream(inputstream); return bitmap; } } catch (exception e) { urlconnection.disconnect(); log.w("imagedownloader", "error downloading image " + url); } { if (urlconnection != null) { urlconnection.disconnect(); } } return null; } @override public void onresume() { mapview.onresume(); super.onresume(); } @override public void ondestroy() { super.ondestroy(); mapview.ondestroy(); } @override public void onlowmemory() { super.onlowmemory(); mapview.onlowmemory(); } private class getjsondata extends asynctask<void, void, string> { @override protected void onpreexecute() { dialog = progressdialog.show(getactivity(), "", getactivity().getresources().getstring(r.string.pleasewait), true); } @override protected string doinbackground(void... arg0) { // creating service handler class instance connectivitymanager conmgr = (connectivitymanager) getactivity().getsystemservice(context.connectivity_service); activenetwork = conmgr.getactivenetworkinfo(); if (activenetwork != null && activenetwork.isconnected()) { servicehandler sh = new servicehandler(); // making request url , getting response string jsonstr = sh.makeservicecall(url, servicehandler.get); log.d("response: ", "> " + jsonstr); if (jsonstr != null) { try { jsonobj = new jsonobject(jsonstr); objectoutput out = new objectoutputstream(new fileoutputstream (new file(getactivity().getcachedir(), "") + file.separator + "cachefile.srl")); out.writeobject(jsonobj.tostring()); out.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } else { log.e("servicehandler", "couldn't data url"); } } else { try { objectinputstream in = new objectinputstream(new fileinputstream (new file(getactivity().getcachedir() + file.separator + "cachefile.srl"))); jsonobj = new jsonobject((string) in.readobject()); in.close(); } catch (classnotfoundexception e) { e.printstacktrace(); } catch (optionaldataexception e) { e.printstacktrace(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (streamcorruptedexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } } if (jsonobj != null) { try { ofrlist = new arraylist<arraylist<string>>(); // getting json array node jsonarray = jsonobj.getjsonarray("offers"); shoplistarray = new arraylist<arraylist<string>>(); // looping through contacts (int = 0; < jsonarray.length(); i++) { jsonobject c = jsonarray.getjsonobject(i); url1 = c.getstring("url"); if (userprefs.getstring("locale", null) == null || userprefs.getstring("locale", null).equals("en")) { desc = c.getstring("desc"); } else { desc = c.getstring("desc_ar"); } expdate = c.getstring("expdate"); ofrdate = c.getstring("date"); shopsarray = c.getjsonarray("shops"); (int n = 0; n < shopsarray.length(); n++) { jsonobject jobj = shopsarray.getjsonobject(n); shopid = jobj.getstring("shopid"); if (shopid.equals(id) && tv3.gettext().tostring().equals("") ) { downloadbitmap(url1); finali = i; getactivity().runonuithread(new runnable() { public void run() { if (activenetwork != null && activenetwork.isconnected()) { image.setimagebitmap(bitmap); savefileincache(string.valueof(finali), bitmap); } else { getfileoutofcache(string.valueof(finali)); image.setimagebitmap(bitmap); } tv3.settext(desc); tv4.settext(getactivity().getresources().getstring(r.string.validtill) + expdate); } }); } } } } catch (jsonexception e) { e.printstacktrace(); } } else { getactivity().runonuithread(new runnable() { public void run() { toast.maketext(getactivity(), getactivity().getresources().getstring(r.string.urnotconnected), toast.length_long).show(); } }); } return desc; } @override protected void onpostexecute(string result) { super.onpostexecute(result); dialog.dismiss(); } } private bitmap getfileoutofcache(string filename) { final string cachepath = getactivity().getcachedir().getpath(); file mydiskcachefilepath = new file(cachepath); file mycachedfile = new file(mydiskcachefilepath + file.separator + filename); if (mycachedfile.exists()) { bitmap = bitmapfactory.decodefile(mycachedfile.tostring()); } else { toast.maketext(getactivity(), getactivity().getresources().getstring(r.string.cachedfilenotexist), toast.length_long).show(); } return bitmap; } public void savefileincache(string afilename, bitmap myimage) { final string cachepath = getactivity().getcachedir().getpath(); file mydisccachefilepath; mydisccachefilepath = new file(cachepath); file mydisccachefile = new file(mydisccachefilepath + file.separator + afilename); try { fileoutputstream out = new fileoutputstream(mydisccachefile); myimage.compress(bitmap.compressformat.jpeg, 80, out); out.close(); } catch (filenotfoundexception e) { e.printstacktrace(); toast.maketext(getactivity(), getactivity().getresources().getstring(r.string.cachefailed), toast.length_short).show(); } catch (ioexception e) { e.printstacktrace(); } }
}
private bitmap downloadbitmap(string url) { httpurlconnection urlconnection = null; try { url uri = new url(url); urlconnection = (httpurlconnection) uri.openconnection(); int statuscode = urlconnection.getresponsecode(); if (statuscode != httpstatus.sc_ok) { return null; } inputstream inputstream = urlconnection.getinputstream(); if (inputstream != null) { bitmap = bitmapfactory.decodestream(inputstream); return bitmap; } } catch (exception e) { urlconnection.disconnect(); log.w("imagedownloader", "error downloading image " + url); } { if (urlconnection != null) { urlconnection.disconnect(); } } return null; } @override public void onresume() { mapview.onresume(); super.onresume(); } @override public void ondestroy() { super.ondestroy(); mapview.ondestroy(); } @override public void onlowmemory() { super.onlowmemory(); mapview.onlowmemory(); } private class getjsondata extends asynctask<void, void, string> { boolean isifcondition=false; @override protected void onpreexecute() { dialog = progressdialog.show(getactivity(), "", getactivity().getresources().getstring(r.string.pleasewait), true); } @override protected string doinbackground(void... arg0) { // creating service handler class instance connectivitymanager conmgr = (connectivitymanager) getactivity().getsystemservice(context.connectivity_service); activenetwork = conmgr.getactivenetworkinfo(); if (activenetwork != null && activenetwork.isconnected()) { servicehandler sh = new servicehandler(); // making request url , getting response string jsonstr = sh.makeservicecall(url, servicehandler.get); log.d("response: ", "> " + jsonstr); if (jsonstr != null) { try { jsonobj = new jsonobject(jsonstr); objectoutput out = new objectoutputstream(new fileoutputstream (new file(getactivity().getcachedir(), "") + file.separator + "cachefile.srl")); out.writeobject(jsonobj.tostring()); out.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } else { log.e("servicehandler", "couldn't data url"); } } else { try { objectinputstream in = new objectinputstream(new fileinputstream (new file(getactivity().getcachedir() + file.separator + "cachefile.srl"))); jsonobj = new jsonobject((string) in.readobject()); in.close(); } catch (classnotfoundexception e) { e.printstacktrace(); } catch (optionaldataexception e) { e.printstacktrace(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (streamcorruptedexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } } if (jsonobj != null) { try { ofrlist = new arraylist<arraylist<string>>(); // getting json array node jsonarray = jsonobj.getjsonarray("offers"); shoplistarray = new arraylist<arraylist<string>>(); // looping through contacts (int = 0; < jsonarray.length(); i++) { jsonobject c = jsonarray.getjsonobject(i); url1 = c.getstring("url"); if (userprefs.getstring("locale", null) == null || userprefs.getstring("locale", null).equals("en")) { desc = c.getstring("desc"); } else { desc = c.getstring("desc_ar"); } expdate = c.getstring("expdate"); ofrdate = c.getstring("date"); shopsarray = c.getjsonarray("shops"); (int n = 0; n < shopsarray.length(); n++) { jsonobject jobj = shopsarray.getjsonobject(n); shopid = jobj.getstring("shopid"); if (shopid.equals(id) && tv3.gettext().tostring().equals("") ) { downloadbitmap(url1); finali = i; //no need use runonuithread since post execute methods runs on main ui thread ..rather set isifcondition=true , job on postexecute method isifcondition=true; desc1=desc; expdate1=expdate; /*getactivity().runonuithread(new runnable() { public void run() { if (activenetwork != null && activenetwork.isconnected()) { image.setimagebitmap(bitmap); savefileincache(string.valueof(finali), bitmap); } else { getfileoutofcache(string.valueof(finali)); image.setimagebitmap(bitmap); } tv3.settext(desc); tv4.settext(getactivity().getresources().getstring(r.string.validtill) + expdate); } });*/ } } } } catch (jsonexception e) { e.printstacktrace(); } } else { //no need use runonuithread since post execute methods runs on main ui thread /*getactivity().runonuithread(new runnable() { public void run() { toast.maketext(getactivity(), getactivity().getresources().getstring(r.string.urnotconnected), toast.length_long).show(); } });*/ } return desc; } @override protected void onpostexecute(string result) { super.onpostexecute(result); dialog.dismiss(); if(isifcondition=true) { if (activenetwork != null && activenetwork.isconnected()) { image.setimagebitmap(bitmap); savefileincache(string.valueof(finali), bitmap); } else { getfileoutofcache(string.valueof(finali)); image.setimagebitmap(bitmap); } tv3.settext(desc1); tv4.settext(getactivity().getresources().getstring(r.string.validtill) + expdate1); } } } private bitmap getfileoutofcache(string filename) { final string cachepath = getactivity().getcachedir().getpath(); file mydiskcachefilepath = new file(cachepath); file mycachedfile = new file(mydiskcachefilepath + file.separator + filename); if (mycachedfile.exists()) { bitmap = bitmapfactory.decodefile(mycachedfile.tostring()); } else { toast.maketext(getactivity(), getactivity().getresources().getstring(r.string.cachedfilenotexist), toast.length_long).show(); } return bitmap; } public void savefileincache(string afilename, bitmap myimage) { final string cachepath = getactivity().getcachedir().getpath(); file mydisccachefilepath; mydisccachefilepath = new file(cachepath); file mydisccachefile = new file(mydisccachefilepath + file.separator + afilename); try { fileoutputstream out = new fileoutputstream(mydisccachefile); myimage.compress(bitmap.compressformat.jpeg, 80, out); out.close(); } catch (filenotfoundexception e) { e.printstacktrace(); toast.maketext(getactivity(), getactivity().getresources().getstring(r.string.cachefailed), toast.length_short).show(); } catch (ioexception e) { e.printstacktrace(); } }
there not need use runonuithread or other thread...because asynctask you..doinbackground work run on bgthread , onpostexecute work on mainuithread..
just check code...i haven't check..but work sure....
Comments
Post a Comment