19

我想将单个字符串传递给异步任务。谁能告诉我它是怎么做的?我的 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;
    }

}
4

2 回答 2

56

你已经有了这个

     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

更新

Asynctask 已被弃用。应该使用 kotlin 协程或 rxjava 或任何其他线程机制作为替代方案。

于 2013-07-09T13:13:37.497 回答
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;
    }
}

在应用程序(ActivityService)中,您可以使用;

private RemoteDataTask mTask;
private void doStuff(){
    String pass = "meow"; // story.get(position).getEntity();
    mTask = new RemoteDataTask(pass);
    mTask.execute();
}
于 2013-07-09T13:24:03.260 回答