Friday, 15 May 2015

How to avoid ANR while long running XML parsing in AsyncTask of android? -



How to avoid ANR while long running XML parsing in AsyncTask of android? -

i downloading files server , parsing , inserting in local db while operation showing progress of operation using progress bar.as files big it's taking 2-4 mins parsing due getting anr on progress bar , progress bar not getting updated after download completed it's showing 50% progress.how avoid anr? attaching code below...

@override protected void onpreexecute() { flag = false; progresscount = 0; progressdialog progressbar = ddownoadprogressbar(context, progresscount); progressbar.show(); } @override protected integer doinbackground(string... urls) { checknetworkconnection(); if (connectionflag) { downloadfile1(); progresscount = 1; publishprogress(progresscount); downloadfile2(); progresscount = 2; publishprogress(progresscount); downloadfile3(); progresscount = 3; publishprogress(progresscount); downloadfile4(); progresscount = 4; publishprogress(progresscount); downloadfile5(); progresscount = 5; publishprogress(progresscount); downloadfile6(); progresscount = 6; publishprogress(progresscount); downloadflag = true; } homecoming null; } @override protected void onprogressupdate(integer... progress) { log.v(tag, "in progressupdate..."); progressbar.setprogress(progresscount); } @override protected void onpostexecute(integer progress) { if (downloadflag) { seek { log.v(tag, "inside onpostexecute...."); parsefile1(); progresscount=7; publishprogress(progresscount); parsefile2(); progresscount=8; publishprogress(progresscount); parsefile3(); progresscount=9; publishprogress(progresscount); parsefile4(); progresscount=10; publishprogress(progresscount); parsefile5(); progresscount=11; publishprogress(progresscount); parsefile6(); progresscount=12; publishprogress(progresscount); progressbar.dismiss(); toast.maketext(synchronizedata.this,"data downloaded server.......", toast.length_long).show(); } grab (exception e) { e.printstacktrace(); } } else { progressbar.dismiss(); toast.maketext( getapplicationcontext(), "file not downloaded....error in network connection...........",toast.length_long).show(); log.v(tag, "file not downloaded..............."); } }

move parsing logic doinbackground method. there's no reason create web calls on background thread , parse on ui thread.

android android-asynctask

No comments:

Post a Comment