-1

I want to create an AsyncTask that will handle my communication with the server (the client is the android app and the server is python). My main activity will need, depends on user interaction, send data to the server. How can I pass the string that changes all the time to the AsyncTask? For example, I have this variable in my main activity:

String toSend = "Something"

The user pressed a button and now the string contains this data:

toSend = "After Button Pressed"

The question is how can I pass the always changing toSend string to the Async Task?

UPDATE:

I know how to create an AsyncTask. The AsyncTask will be started at the beginning of the activity. It is not a private class in the activity. The input to the AsyncTask is dinamically changing (based in user interacts). Is there a way to have a dinamically changing input to the task? Maybe pass it by ref?

  • 1
    Possible duplicate of [How to pass variables in and out of AsyncTasks?](https://stackoverflow.com/questions/9900834/how-to-pass-variables-in-and-out-of-asynctasks) – Vidhi Dave Mar 16 '18 at 09:17
  • `How can I pass the string that changes all the time to the AsyncTask? `. Impossible. You cannot pass parameters to an already runing asynctask. You can only pass parameters at startup of your task. – greenapps Mar 16 '18 at 09:19
  • But the running asynctask, if it is a private class of your activity, has access to String variables declared in the activity. So there the code could look if the content of your string had changed. – greenapps Mar 16 '18 at 09:21
  • This you can achieve using runnable with some delay, & when user press or you set some debounce , you need to cancel previous thread data & send latest one. – Sandeep Sankla Mar 16 '18 at 09:32
  • So, @greenapps you say this can be achieved only if the async task is part of the main activity class? – Gili Jacobi Mar 16 '18 at 09:50
  • You have not confirmed that you want to use an already running asynctask. – greenapps Mar 16 '18 at 09:52
  • And you have not commented the answers that let you start an asynctask. So what is going on? – greenapps Mar 16 '18 at 09:53
  • can I pass the parameter by ref? this way the parameter will change in the async task – Gili Jacobi Mar 16 '18 at 09:55
  • `You have not confirmed that you want to use an already running asynctask. ` – greenapps Mar 16 '18 at 09:57
  • I confirm it now, also I commented the other answers – Gili Jacobi Mar 16 '18 at 10:07
  • Then update the subject and text of your post. You confused all. Those answers would not have been given if you had been clear about what you wanted. – greenapps Mar 16 '18 at 10:10
  • I updated the post. Is it better? Do you think pass by ref will work? – Gili Jacobi Mar 16 '18 at 10:15

2 Answers2

0

By declaring your String as final you cannot change its value. So, Declare it as

final String toSend = "After Button Pressed";
Xay
  • 248
  • 3
  • 10
0

Create a constructor in your AsyncTask class and send the inpur parameters within it, Ex:

private class ExampleAsyncTask extends AsyncTask<Void,Void, Object>{
    String inputString;
    public ExampleAsyncTask(String inputString){
        this.inputString = inputString;
    }

    @Override
    protected void onPreExecute(){

    }

    @Override
    protected Object doInBackground(String... params){
    //call your server here by passing the variable (this.inputString)
    return result;
    }

    @Override
    protected void onPostExecute(Object result){

    }
}

//your asynctask calling part should be like this

button.setOnClickListener(new View.OnCLickListener(){
    new ExampleAsyncTask(this.toSend).execute();
});
Bethan
  • 971
  • 8
  • 23