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

}
1
  • 3
    pass params[0] instead of params to getEntity method for more information see Varargs Commented Jul 9, 2013 at 13:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
@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 stackoverflow.com/questions/4732544/…. You can make the variable a member of class initialize before use to overcome this
when I try to call new RemoteDataTask().execute it says "Cannot resolve symbol 'execute'". can you show me a way to solve this
@AbdulhakimZeinu you should ask a new question
6

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.