I am writing an application for Android that needs to respond as fast as possible. The app basically grabs content from a website (HTML, not XHTML
) and parses some of its information using XPATH
queries. I want the user to be able to hit cancel and abort the request, returning to the previous activity.
Right now my code looks like this (I use the htmlcleaner jar from http://htmlcleaner.sourceforge.net/ to convert html to xhtml
):
//this is called from the doInBackground() method embedded in an ASyncTask
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties props = cleaner.getProperties();
props.setAllowHtmlInsideAttributes(true);
props.setAllowMultiWordAttributes(true);
props.setRecognizeUnicodeChars(true);
props.setOmitComments(true);
try {
URL url = new URL("---my-url-goes-here---");
URLConnection conn = url.openConnection();
TagNode node = cleaner.clean(new InputStreamReader(conn.getInputStream()));
return node;
} catch (Exception e) {
failed = true;
}
I am not sure if the downloading in my code is actually abortable, let alone that I would know how to accomplish this. Am I on the right way or should I go for a different approach?
I've solved this, see this post for more information on using ASyncTask. Android stop download