java - using AsyncTask for multiple actions -
I'm working on Android.
There is a parcel class in my main activity. Java that extends asyncTask:
The class parses a URL and does some work with it.
Private Class ParseURL AsyncTask and String, Zero, String & gt; {@Override protected string doInBackground (string ... strings) {stringbuff buffer = new stringbuffer (); Document document; Try {Log.i ("Output", "Trying to Connect .."); Doc = Jsoup.connect ("http://sirim.co.il/") .get (); Log.i ("Output", "Connect"); Elements meteillems = doc.select ("a [class = mainlevelmatkonim]"); (Element Metaelem: MetaLames) for {string c = metaelem.txt (); Buffer.Append (C + "\ r \ n"); }} Hold (IOException e) {// TODO auto-generated cal block e.printStackTrace (); } Return buffer. Tosting (); } @OverridePostExecute protected at zero (strings) {super.onPostExecute (S); HttpInfo.setText (s); }}
My question is - If I want to use an asynchronous tool to parse a page again - but take different actions with the parsed page - < / P>
Should I use the same class or create a new one?
What is the correct way? If there is a "if" statement or anything else to see what to do with a page with a purse? Or some of the Asinca Task Classes that work differently?
Thanks in advance, use
Use a parameter for your work :
ParseURL increases the private class AsyncTask & lt; String, zero, string & gt; {@Override protected string doInBackground (string ... url) {StringBuffer buffer = new stringbuffer (); Document document; Try {Log.i ("Output", "Trying to Connect .."); Get Doc = Jsoup.connect (url [0]) (); Log.i ("Output", "Connect"); Elements meteillems = doc.select ("a [class = mainlevelmatkonim]"); (Element Metaelem: MetaLames) for {string c = metaelem.txt (); Buffer.Append (C + "\ r \ n"); }} Hold (IOException e) {// TODO auto-generated cal block e.printStackTrace (); } Return buffer. Tosting (); } @OverridePostExecute protected at zero (strings) {super.onPostExecute (S); HttpInfo.setText (s); }}
Use it like this:
Parcel function = new parcel (); Task.execute ("http://sirim.co.il/");
But you will need to reproduce the task
The task can only be executed once (if a second execution is tried An exception will be inserted.)
You can create a helper in the Private class, ParseURL increases AsyncTask
; String, zero, string & gt; {Public static zero pars (string url) {parcel function = new parcel (); Task.execute (url); Return work; } // [...]
and use it like this:
ParseURL task = ParseURL.parse ("http: // sirim. Co .il / "); Increases the Private Class ParseURL AsyncTask & Lt; String, wide, string & gt; {PUBLIC ENEMUM PERSONALICATION {ACTION1, ACTION2, ACTION3}; Public static zero parsing (string url, parsurrection action) {parcel function = new parcel (verb); Task.execute (url); Return work; } Private Pursure Action; Public ParseURL (ParseURLAction action) {this.action = Action; } // [...] @Override Secure Zero PostExecute (string) {super.onPostExecute (S); Switch (action) {case ACTION1: // to break something; // [...]}
Usage:
ParseURL works = ParseURL.parse ("http://sirim.co.il/" , Parcel Parasercarcle, action 1);
Comments
Post a Comment