android - Showing ProgressBar on parsing and downloading json result -
in app hitting service can have no result n number of results(basically barcodes). of using default circular progressbar when json parsed , result being saved in local db(using sqlite). if json has large number of data takes 30-45 min parse , simultaneously saving data in db, makes interface unresponsive period of time , makes user think app has broken/hanged. problem want show progressbar percentage stating how data parsed , saved user know app still working , not dead. took link couldn't find how achieve. here's asynctask,
class backgroundtasks extends asynctask<string, string, void> { private string operation, itemref; private arraylist<model_barcodedetail> changedbarcodelist, barcodelist; private arraylist<string> changereflist; string page; public backgroundtasks(string operation, string itemref, string page) { this.operation = operation; this.itemref = itemref; this.page = page; } @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); if (dialog == null) { dialog = progressdialog.show(mactivity, null, "please wait ...", true); } } @override protected void doinbackground(string... params) { // todo auto-generated method stub try{ if (!connection.havenetworkconnection()) { dialog.dismiss(); connection.showtoast(screensize, "no internet connection."); return null; } if (operation.equalsignorecase("downloadchangeitemref")) { changereflist = downloadchangeitemref(params[1]); if (changereflist != null && !changereflist.isempty()) { reflist1.addall(changereflist); } } if ((changereflist != null && changereflist.size() >0)) { setupdatedbarcodes(changedbarcodelist); } } } catch (exception e) { e.printstacktrace(); } return null; } @suppresslint("simpledateformat") @override protected void onpostexecute(void result) { super.onpostexecute(result); } } arraylist<string> downloadchangeitemref(string api_token) { arraylist<string> changedreflist = null; httpclient httpclient = new defaulthttpclient(); httppost postrequest = new httppost(thoth_url + "/" + todaysdate + "?&return=json"); string url = thoth_url + "/" + todaysdate + "?&return=json"; string result = ""; try { changedreflist = new arraylist<string>(); responsehandler<string> responsehandler = new basicresponsehandler(); result = httpclient.execute(postrequest, responsehandler); jsonobject jsonobj = new jsonobject(result); jsonarray jsonarray = jsonobj.getjsonarray("changes"); if (jsonarray.length() == 0) { return null; } (int = 0; < jsonarray.length(); i++) { jsonobject obj = jsonarray.getjsonobject(i); changedreflist.add(obj.getstring("ref")); } } catch (illegalstateexception e) { // todo auto-generated catch block // when there no thoth url log.i("inclient: ", e.getmessage()); return null; } catch (exception e) { // when there no itemref return null; } return changedreflist; } private boolean setupdatedbarcodes( final arraylist<model_barcodedetail> changedbarcodelist2) { try { barcodedatabase barcodedatabase = new barcodedatabase(mactivity); barcodedatabase.open(); (model_barcodedetail model : changedbarcodelist2) { barcodedatabase.updateentry(model, userid); } n++; barcodedatabase.close(); if (reflist1.equals(reflist)) { if (dialog != null) { dialog.dismiss(); } connection.showtoast(screensize, "barcodes updated successfully"); } } catch (exception e) { log.i("exception caught in: ", "setdownloadedbarcodes method"); e.printstacktrace(); return false; } return true; }
Comments
Post a Comment