Android Asynctask传递单个字符串

时间:2013-07-09 13:11:11

标签: android android-asynctask

我想将一个字符串传递给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;
    }

}

2 个答案:

答案 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;
    }
}

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

private RemoteDataTask mTask;
private void doStuff(){
    String pass = "meow"; // story.get(position).getEntity();
    mTask = new RemoteDataTask(pass);
    mTask.execute();
}