Android Asynctask passing a single string
You can build AsyncTask
with a constructor.
public class RemoteDataTask extends AsyncTask<String, String, Long> {
private String data;
public RemoteDataTask(String passedData) {
data = passedData;
}
@Override
protected String doInBackground(Context... params) {
// you can access "data" variable here.
EntityUtils.getEntity(activity, params, new EntityGetListener() {
@Override
public void onGet(Entity entity) {
viewcount = entity.getEntityStats().getViews();
}
@Override
public void onError(SocializeException error) {
}
});
return null;
}
}
In the application (Activity
, Service
etc), you can use;
private RemoteDataTask mTask;
private void doStuff(){
String pass = "meow"; // story.get(position).getEntity();
mTask = new RemoteDataTask(pass);
mTask.execute();
}
You already have this
new RemoteDataTask().execute(pass); // assuming pass is a string
In doInbackground
@Override
protected Long doInBackground(String... params) {
String s = params[0]; // here's youre string
... //rest of the code.
}
You can find more info @
http://developer.android.com/reference/android/os/AsyncTask.html
Update
Asynctask is depecated. Should be using kotlin coroutines or rxjava or any other threading mechanism as alternatives.