(Not getting answers on StackOverflow [
http://stackoverflow.com/questions/5429455/xmlpullparser-in-a-thread-returns-nothing-every-other-run-wh
] , so I thought I'd try here.)

I have a simple `XmlPullParser` in a non-UI thread that parses some
data from an XML over HTTP and stores it in a local MySQL database.
The first time around, the parsing operation goes beautifully and all
the data ends up where it should. But without fail, if I start the
parse operation again (assuming the activity hasn't been destroyed and
recreated in the meantime), it returns nothing. It doesn't throw up
any exceptions; rather it acts as if it's already at the end of the
file and simply finishes without finding any tags. Then if I run it a
third time, it works fine again, and the cycle repeats.

Is there a method I should be calling to properly close the parser
when it's finished? I looked around in the docs and examples but
couldn't find anything about it.

Here's a cut-down version of the relevant code:

    parseThread = new Thread () {
        public void run() {

            mDB.delete(directoryPeople.TEMP_TABLE, null, null);

            XmlPullParserFactory parserCreator =
XmlPullParserFactory.newInstance();
            XmlPullParser parser = parserCreator.newPullParser();

            URL feed = new URL(directoryUrl);
            parser.setInput(feed.openStream(), null);

            ContentValues entry = new ContentValues();
            int parserEvent = parser.getEventType();
            String tag = "";
            String value = "";
            entryCount = 0;

            while (parserEvent != XmlPullParser.END_DOCUMENT) {

                if(parserEvent == XmlPullParser.START_TAG) {
                    tag = parser.getName();
                    if (isPeopleField(tag)) {
                        parserEvent = parser.next();
                        value =  parser.getText();
                        entry.put(tag, value.replaceAll("\n", ""));
                    }
                }
                if(parserEvent == XmlPullParser.END_TAG &&
parser.getName().compareTo("ROW") == 0) {
                    mDB.insert(directoryPeople.TEMP_TABLE, null,
entry);
                    entryCount++;
                    hUpdateProgressBar.sendEmptyMessage(0);
                    entry.clear();
                }
                 parserEvent = parser.next();
            }

            /** Copy temp table to permanent table */
            mDB.delete(directoryPeople.PEOPLE_TABLE, null, null);

            String copyQuery = "INSERT INTO " +
directoryPeople.PEOPLE_TABLE
                + " SELECT * FROM " + directoryPeople.TEMP_TABLE;

            mDB.execSQL(copyQuery);

            /** Delete contents of temp table */
            mDB.delete(directoryPeople.TEMP_TABLE, null, null);

            mHandler.post(new Runnable() {
            public void run() {
                    adapter.getCursor().requery();
                    }
            });
        }
    };
    parseThread.start();

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to