我想将一个字符串传递给asynctask。谁能告诉我它是如何完成的?我的getEntity需要方法getEntity(Activity,String,EntityGetListener)但我继续传递这个String []
String pass= story.get(position).getEntity();
new RemoteDataTask().execute(pass);
private class RemoteDataTask extends AsyncTask<String, String, Long> {
@Override
protected Long doInBackground(String... params) {
// TODO Auto-generated method stub
EntityUtils.getEntity(activity, params, new EntityGetListener() {
@Override
public void onGet(Entity entity) {
viewcount = entity.getEntityStats().getViews();
}
@Override
public void onError(SocializeException error) {
}
});
return null;
}
}
答案 0 :(得分:52)
你已经有了这个
new RemoteDataTask().execute(pass); // assuming pass is a string
在doInbackground
@Override
protected Long doInBackground(String... params) {
String s = params[0]; // here's youre string
... //rest of the code.
}
您可以找到更多信息@
http://developer.android.com/reference/android/os/AsyncTask.html
答案 1 :(得分:5)
您可以使用构造函数构建AsyncTask
。
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;
}
}
在应用程序(Activity
,Service
等)中,您可以使用;
private RemoteDataTask mTask;
private void doStuff(){
String pass = "meow"; // story.get(position).getEntity();
mTask = new RemoteDataTask(pass);
mTask.execute();
}