Note that there are some explanatory texts on larger screens.

plurals
  1. POXml parsing with AsyncTask slower than parsing in main Thread
    primarykey
    data
    text
    <p>I'm having a strange behaviour in my code: parsing a local xml file on simulator with asyncTask takes longer than parsing it on main thread.</p> <p>Here is my code with AsyncTask:</p> <pre><code>public class MostraTutti extends SherlockActivity { ListView lv; final List&lt;ListViewItem&gt; items = new ArrayList&lt;MostraTutti.ListViewItem&gt;(); final ArrayList&lt;String&gt; nome = new ArrayList&lt;String&gt;(); final ArrayList&lt;String&gt; immagine = new ArrayList&lt;String&gt;(); ... 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&lt;String, Void, String&gt; { 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"); for (int i = 0; i &lt; 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&lt;?&gt; 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 the menu; this adds items to the action bar if it is present. getSupportMenuInflater().inflate(R.menu.activity_main, menu); return true; } class ListViewItem { public int ThumbnailResource; public String Title; public String SubTitle; } } </code></pre> <p>And here's my code with no AsyncTask:</p> <pre><code>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&lt;ListViewItem&gt; items = new ArrayList&lt;MostraTutti.ListViewItem&gt;(); final ArrayList&lt;String&gt; nome = new ArrayList&lt;String&gt;(); final ArrayList&lt;String&gt; immagine = new ArrayList&lt;String&gt;(); ... final int array_image2[] ={R.drawable.iodocloroidrossichinolina,R.drawable.acidoacetilsalicilico, ...}; </code></pre> <p>InputStream xmlFile = getResources().openRawResource(R.raw.sostanze);</p> <pre><code> 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"); for (int i = 0; i &lt; 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&lt;?&gt; 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; } } </code></pre> <p>The difference on simulator is 16 seconds for the main thread one and 1 minute for the AsyncTask one!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload