Reputation: 158
I have a quastion about an AsyncTask, I hope somone could give me some advice. I am performing AsyncTasks to get data from internet. The task is in a separate class. I have multiple AsyncTasks that do exactly the same action, but they get called at different moments and pass data back to different methods. In the current AsyncTask i can exucute it and give one or more url's as parameter.
My question is: is there a way to send an extra parameter to the AsyncTask so that I can use a switch case in the onPostExecute to decide where the webdata should go?
This is one of the AsyncTask classes:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import android.os.AsyncTask;
import android.util.Log;
public class GetContactpersoonFromWeb extends AsyncTask<String, Void, String> {
String result = "";
String webData;
FirstScreen activity;
public GetContactpersoonFromWeb(FirstScreen activity)
{
this.activity = activity;
}
protected String doInBackground(String... urls) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(urls[0]);
HttpResponse response;
try {
response = httpClient.execute(httpGet, localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = null; //vanaf niks beginnen
while ((line = reader.readLine()) != null) {
result += line + "\n";
}
this.webData = result;
}
catch (Exception e) {
Log.d("info","error bij het ophalen van de data", e);
}
return this.webData;
}
protected void onPostExecute(String result) {
this.webData = result;
activity.getContactpersoonData(webData);
}
}
The difference with the other AsyncTasks is the last rule: activity.getFabricData(webData);
In an other class it is: activity.getMachineData(webData);
etc..
Does anyone have an idea how to achieve this by using only 1 class? I think using 7 classes with only 1 rule of diffrence is not the best way.
Thanks in advance!
Upvotes: 0
Views: 774
Reputation: 169
@insomniac's answer will work fine, but there's another option to consider. Provided you know where the data needs to go before creating the AsyncTask
, another option for different onPostExecute()
behavior is to use polymorphism rather than a switch
. This allows you to add another type of behavior in the future without the risk of breaking existing code. The technique is explained here: http://sourcemaking.com/refactoring/replace-conditional-with-polymorphism
In your case you'd move the code from GetContactpersoonFromWeb
to an abstract base class, say, GetFromWeb
, then have GetContactpersoonFromWeb
inherit from GetFromWeb
and override onPostExecute()
. Repeat for FabricData
and MachineData
.
Upvotes: 0
Reputation: 11756
You need to create an extra variable in the AsyncTask
and add the extra parameter in the constructor like
public GetContactpersoonFromWeb(FirstScreen activity,int extra)
{
this.activity = activity;
this.extra=extra;
}
Then you can use this value in a switch case like
switch(extra){`
................
.................
Upvotes: 1
Reputation: 13520
Add the date based on which you will perform your switch
in your constructor
public GetContactpersoonFromWeb(FirstScreen activity,int yourExtraData)
{
this.activity = activity;
mExtraData = extraData;
}
and then use mExtraData
in your onPostExecute
Upvotes: 2