I parse the XML data and put it in an object. This takes quite some time, and I decided to run it in the background using AsyncTask. My code is almost exactly the same:
How to get XML using AsyncTask and Timer?
The difference is that I want to publish Progress, as I take a long time. Relevant part of the code in which I want to post the progress:
@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
if (localName.equalsIgnoreCase("client")) {
clients.addClient(mClient);
RotationAwareTask.publishProgress();
mClient = null;
}
}
Now you would understand that I am a beginner. The closest study, which is consistent with what I want to do, says:
http://www.mail-archive.com/ android-developers@googlegroups.com /msg79275.html
Yes, this is a little more complicated ... Two approaches:
1) AsyncTask org.sax.ContentHandler. , SAX AsyncTask.publishProgress().
2) , AsyncTask .
, :
publishProgress (...) AsyncTask -
, publicProgress endElement? - ? :
XML.
- .
SAXParser .
endElement , .
, AsyncTask, , Commonsware .
, , .
:
@Cristian - :
static class RotationAwareTask extends AsyncTask<Void, Void, Void> {
RotationAsync activity = null;
int progress = 0;
private SaxClientsHandler saxClientsHandler;
RotationAwareTask(RotationAsync activity) {
attach(activity);
}
@Override
protected Void doInBackground(Void... unused) {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
saxClientsHandler = new SaxClientsHandler();
xr.setContentHandler(saxClientsHandler);
InputSource mSource = new InputSource( new StringReader( Whmcs.getClientsXml() ) );
xr.parse(mSource);
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
return(null);
}
@Override
protected void onProgressUpdate(Void... unused) {
if (activity==null) {
Log.w("RotationAsync", "onProgressUpdate() skipped – no activity");
}
else {
progress += 5;
activity.updateProgress(progress);
}
}
@Override
protected void onPostExecute(Void unused) {
if (activity == null) {
Log.w("RotationAsync", "onPostExecute() skipped – no activity");
}
else {
activity.markAsDone();
}
}
void detach() {
activity = null;
}
void attach(RotationAsync activity) {
this.activity = activity;
}
int getProgress() {
return(progress);
}
}
Commonsware:
https://github.com/commonsguy/cw-android/blob/master/Rotation/RotationAsync/src/com/commonsware/android/rotation/async/RotationAsync.java
:
saxClientsHandler = SaxClientsHandler();
SaxClientHandler(), endElement. .
@Mayra, saxClientHandler AsyncTask?