19

I would like to pass a single string into an asynctask. Could anyone show me how it is done? my getEntity needs The method getEntity(Activity, String, EntityGetListener) but I keep passing this 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;
    }

}
KC Chai
  • 1,607
  • 9
  • 30
  • 59
  • 3
    pass `params[0]` instead of `params` to `getEntity` method for more information see [Varargs](http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html) – ρяσѕρєя K Jul 09 '13 at 13:13

2 Answers2

56

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.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • When passing the `String value` into `AsyncTask` class, It says the variable is accessed from within an inner class. Need to be declared as final. But I need change variable values time to time. How can I fix this issue – E J Chathuranga Sep 19 '17 at 03:52
  • @EJChathuranga if you are accessing a variable that is not a class variable and it is accessed in annonymous inner class it has to be final. Read https://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class. You can make the variable a member of class initialize before use to overcome this – Raghunandan Sep 19 '17 at 03:56
  • when I try to call new RemoteDataTask().execute it says "Cannot resolve symbol 'execute'". can you show me a way to solve this – Abdulhakim Zeinu Apr 04 '19 at 09:45
  • @AbdulhakimZeinu you should ask a new question – Raghunandan Apr 05 '19 at 04:26
5

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();
}
az3
  • 3,571
  • 31
  • 31