android - Xml parsing with AsyncTask slower than parsing in main Thread -
this question has answer here:
i'm having strange behaviour in code: parsing local xml file on simulator asynctask takes longer parsing on main thread.
here code asynctask:
public class mostratutti extends sherlockactivity { listview lv; final list<listviewitem> items = new arraylist<mostratutti.listviewitem>(); final arraylist<string> nome = new arraylist<string>(); final arraylist<string> immagine = new arraylist<string>(); ... final int array_image2[] ={r.drawable.iodocloroidrossichinolina,r.drawable.acidoacetilsalicilico, ...}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mostra_tutti); lv = (listview) findviewbyid(r.id.listview); getsupportactionbar().setdisplayshowhomeenabled(false); rssfeedtask rsstask = new rssfeedtask(); rsstask.execute(); } private class rssfeedtask extends asynctask<string, void, string> { private progressdialog dialog; string response = ""; @override protected void onpreexecute() { dialog = new progressdialog(mostratutti.this); dialog.setmessage("leggo le sostanze..."); dialog.show(); } @override protected string doinbackground(string... urls) { inputstream xmlfile = getresources().openrawresource(r.raw.sostanze); try { documentbuilderfactory documentbuilderfactory = documentbuilderfactory.newinstance(); documentbuilder documentbuilder = documentbuilderfactory.newdocumentbuilder(); final document document = documentbuilder.parse(xmlfile); document.getdocumentelement().normalize(); nodelist nodelist = document.getelementsbytagname("sostanza"); (int = 0; < nodelist.getlength(); i++) { final int indice = i; nome.add(document.getelementsbytagname("nome").item(indice).gettextcontent()); iupac.add(document.getelementsbytagname("iupac").item(indice).gettextcontent()); aspetto.add(document.getelementsbytagname("aspetto").item(indice).gettextcontent()); formula.add(document.getelementsbytagname("formula").item(indice).gettextcontent()); immagine.add(document.getelementsbytagname("immagine").item(indice).gettextcontent()); appartenenza.add(document.getelementsbytagname("appartenenza").item(indice).gettextcontent()); spiegazione.add(document.getelementsbytagname("spiegazione").item(indice).gettextcontent()); tempfus.add(document.getelementsbytagname("temperaturafusione").item(indice).gettextcontent()); tempeboll.add(document.getelementsbytagname("temperaturaebollizione").item(indice).gettextcontent()); solubilita.add(document.getelementsbytagname("solubilita").item(indice).gettextcontent()); note.add(document.getelementsbytagname("eccezioni").item(indice).gettextcontent()); string str = document.getelementsbytagname("formula").item(indice).gettextcontent(); str = str.replaceall("0", "\u2080"); str = str.replaceall("1", "\u2081"); str = str.replaceall("2", "\u2082"); str = str.replaceall("3", "\u2083"); str = str.replaceall("4", "\u2084"); str = str.replaceall("5", "\u2085"); str = str.replaceall("6", "\u2086"); str = str.replaceall("7", "\u2087"); str = str.replaceall("8", "\u2088"); str = str.replaceall("9", "\u2089"); final string stringa = str; formulaconvertita.add(stringa); //customlistviewadapter adapter = new customlistviewadapter(mostratutti.this,items); //lv.setadapter(adapter); items.add(new listviewitem() {{ thumbnailresource = array_image2[indice]; title = document.getelementsbytagname("nome").item(indice).gettextcontent(); subtitle = stringa; }}); }; } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (parserconfigurationexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (saxexception e) { // todo auto-generated catch block e.printstacktrace(); } return response; } @override protected void onpostexecute(string result) { dialog.dismiss(); customlistviewadapter adapter = new customlistviewadapter(mostratutti.this,items); lv.setadapter(adapter); lv.setonitemclicklistener( new onitemclicklistener() { public void onitemclick(adapterview<?> arg0, view v, int position, long id) { context context = getbasecontext(); intent myintent = new intent(context, dettagli.class); myintent.putextra("nome_sostanza",nome.get(position)); // myintent.putextra("formula",formula.get(position)); myintent.putextra("iupac",iupac.get(position)); myintent.putextra("aspetto",aspetto.get(position)); myintent.putextra("appartenenza",appartenenza.get(position)); myintent.putextra("solubilita",solubilita.get(position)); myintent.putextra("tempfus",tempfus.get(position)); myintent.putextra("tempeboll",tempeboll.get(position)); myintent.putextra("spiegazione",spiegazione.get(position)); myintent.putextra("immagine", array_image2[position]); myintent.putextra("formulaconvertita", formulaconvertita.get(position)); myintent.putextra("note", note.get(position)); startactivityforresult(myintent, 0); } } ); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getsupportmenuinflater().inflate(r.menu.activity_main, menu); return true; } class listviewitem { public int thumbnailresource; public string title; public string subtitle; } }
and here's code no asynctask:
public class mostratutti extends sherlockactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.mostra_tutti); getsupportactionbar().setdisplayshowhomeenabled(false); final listview lv = (listview) findviewbyid(r.id.listview); final list<listviewitem> items = new arraylist<mostratutti.listviewitem>(); final arraylist<string> nome = new arraylist<string>(); final arraylist<string> immagine = new arraylist<string>(); ... final int array_image2[] ={r.drawable.iodocloroidrossichinolina,r.drawable.acidoacetilsalicilico, ...};
inputstream xmlfile = getresources().openrawresource(r.raw.sostanze);
try { documentbuilderfactory documentbuilderfactory = documentbuilderfactory.newinstance(); documentbuilder documentbuilder = documentbuilderfactory.newdocumentbuilder(); final document document = documentbuilder.parse(xmlfile); document.getdocumentelement().normalize(); //taghandler.handlechanneltag(document); nodelist nodelist = document.getelementsbytagname("sostanza"); (int = 0; < nodelist.getlength(); i++) { final int indice = i; nome.add(document.getelementsbytagname("nome").item(indice).gettextcontent()); iupac.add(document.getelementsbytagname("iupac").item(indice).gettextcontent()); aspetto.add(document.getelementsbytagname("aspetto").item(indice).gettextcontent()); formula.add(document.getelementsbytagname("formula").item(indice).gettextcontent()); immagine.add(document.getelementsbytagname("immagine").item(indice).gettextcontent()); appartenenza.add(document.getelementsbytagname("appartenenza").item(indice).gettextcontent()); spiegazione.add(document.getelementsbytagname("spiegazione").item(indice).gettextcontent()); tempfus.add(document.getelementsbytagname("temperaturafusione").item(indice).gettextcontent()); tempeboll.add(document.getelementsbytagname("temperaturaebollizione").item(indice).gettextcontent()); solubilita.add(document.getelementsbytagname("solubilita").item(indice).gettextcontent()); note.add(document.getelementsbytagname("eccezioni").item(indice).gettextcontent()); string str = document.getelementsbytagname("formula").item(indice).gettextcontent(); str = str.replaceall("0", "\u2080"); str = str.replaceall("1", "\u2081"); str = str.replaceall("2", "\u2082"); str = str.replaceall("3", "\u2083"); str = str.replaceall("4", "\u2084"); str = str.replaceall("5", "\u2085"); str = str.replaceall("6", "\u2086"); str = str.replaceall("7", "\u2087"); str = str.replaceall("8", "\u2088"); str = str.replaceall("9", "\u2089"); final string stringa = str; formulaconvertita.add(stringa); items.add(new listviewitem() {{ thumbnailresource = array_image2[indice]; title = document.getelementsbytagname("nome").item(indice).gettextcontent(); subtitle = stringa; }}); } } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } catch (parserconfigurationexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (saxexception e) { // todo auto-generated catch block e.printstacktrace(); } customlistviewadapter adapter = new customlistviewadapter(this,items); lv.setadapter(adapter); lv.setonitemclicklistener( new onitemclicklistener() { public void onitemclick(adapterview<?> arg0, view v, int position, long id) { context context = getbasecontext(); intent myintent = new intent(context, dettagli.class); myintent.putextra("nome_sostanza",nome.get(position)); // myintent.putextra("formula",formula.get(position)); myintent.putextra("iupac",iupac.get(position)); myintent.putextra("aspetto",aspetto.get(position)); myintent.putextra("appartenenza",appartenenza.get(position)); myintent.putextra("solubilita",solubilita.get(position)); myintent.putextra("tempfus",tempfus.get(position)); myintent.putextra("tempeboll",tempeboll.get(position)); myintent.putextra("spiegazione",spiegazione.get(position)); myintent.putextra("immagine", array_image2[position]); myintent.putextra("formulaconvertita", formulaconvertita.get(position)); myintent.putextra("note", note.get(position)); startactivityforresult(myintent, 0); } } ); } @override public boolean oncreateoptionsmenu(menu menu) { getsupportmenuinflater().inflate(r.menu.activity_main, menu); return true; } class listviewitem { public int thumbnailresource; public string title; public string subtitle; } }
the difference on simulator 16 seconds main thread 1 , 1 minute asynctask one!
i've marked question duplicate last time did this, reviewers voted against it.
either way, reason asynctask
's doinbackground()
runs in backgroud priority class , has share @ 10% of cpu time other background tasks, such rss reader's etc -- no matter how idle system is.