XmlParser too slow

0

In my application it reads an XML with 3 elements, and populates a listview . The problem is that it is very slow, even with very little data in XML, I did a test with the XML with 3 data, and even though it was slow, it takes about 4 seconds to load the list, see the project:

public class Segmento {

    private String id;
    private String nome;
    private String subtitulo;
    private String imagem;
    private String link;

    //construtor
    //get e set

}

Class to read the reported URL:

public class XmlReader {

    private String rssUrl;

    /**
     * Constructor
     */
    public XmlReader(String rssUrl) {
        this.rssUrl = rssUrl;

    }

    /**
     * Pega uma lista de XML.
     * 
     * @return
     */
    public List<Segmento> getItems() throws Exception {
        // SAX parse RSS data
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        XmlParseHandler handler = new XmlParseHandler();
        saxParser.parse(rssUrl, handler);
        return handler.getItems();
    }

}

Class that handles XML items:

public class XmlParseHandler extends DefaultHandler {

    private List<Segmento> rssItems;

    // Used to reference item while parsing
    private Segmento currentItem;


    private boolean parsingId;

    private boolean parsingNome;
    private boolean parsingSubtitulo;
    private boolean parsingLink;

    public XmlParseHandler() {
        rssItems = new ArrayList<Segmento>();
    }

    public List<Segmento> getItems() {
        return rssItems;
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if ("empresa".equals(qName)) {
            currentItem = new Segmento();
        } else if ("id".equals(qName)) {
            parsingId = true;
        } else if ("nome".equals(qName)) {
            parsingNome = true;
        }
        else if ("subtitulo".equals(qName)) {
            parsingSubtitulo = true;
        }
        else if ("link".equals(qName)) {
            parsingLink = true;
        }

    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("empresa".equals(qName)) {
            rssItems.add(currentItem);
            currentItem = null;
        } else if ("id".equals(qName)) {
            parsingId = false;
        } else if ("nome".equals(qName)) {
            parsingNome = false;
        }
        else if ("subtitulo".equals(qName)) {
            parsingSubtitulo = false;
        }
        else if ("link".equals(qName)) {
            parsingLink = false;
        }

    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (parsingId) {
            if (currentItem != null)
                currentItem.setId(new String(ch, start, length));
        } else if (parsingNome) {
            if (currentItem != null) {
                currentItem.setNome(new String(ch, start, length));
                parsingNome = false;
            }
        }
            else if (parsingSubtitulo) {
                if (currentItem != null) {
                    currentItem.setSubtitulo(new String(ch, start, length));
                    parsingSubtitulo = false;
                }
            }
                else if (parsingLink) {
                    if (currentItem != null) {
                        currentItem.setLink(new String(ch, start, length));
                        parsingLink = false;
                    }
                }
    }
}

Main class, with menu will be chosen an item in list , and it calls another activity that read XML:

public void onItemClick(AdapterView<?> arg0, View arg1, int posicao, long arg3) {

    //Pega o item que foi selecionado.
    Intent telaSegmento = new Intent(MainActivity.this, SegmentoView.class);
    startActivity(telaSegmento);
}

Activity that read the XML and fills in the list, I believe it is here where the delay is, at the moment it is called, in oncreate the following method is already called:

private void criarListView() throws Exception {
    //Carregar XML
    reader = new XmlReader("xmllink");

    if(reader.getItems().size()==0){

        msgItemNulo();//metodo de mensagem informando que nada foi encontrado!
    }

    itensEmpresa = new ArrayList<Segmento>(); //instancia a lista

    for (int i = 0; i < reader.getItems().size(); ++i) {
        Segmento segmento = new Segmento();
        segmento.setId(reader.getItems().get(i).getId());
        segmento.setNome(reader.getItems().get(i).getNome());
        segmento.setSubtitulo(reader.getItems().get(i).getSubtitulo());
        segmento.setLink(reader.getItems().get(i).getLink());

                    itensEmpresa.add(segmento);

    }


    // Cria o adapter
    adapterEmpresa = new AdapterSegmento(this, itensEmpresa);

    // Define o Adapter
    listView.setAdapter(adapterEmpresa);
    // Cor quando a lista é selecionada para ralagem.
    listView.setCacheColorHint(Color.TRANSPARENT);
}

Right delay of about 5 seconds just to load 3 data.

    
asked by anonymous 30.11.2014 / 21:33

0 answers