Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynctask: pass two or more values from doInBackground to onPostExecute

Tags:

I have an Asynctask which retrieves two int vaules and i want to pass them to onPostExecute to show them on the view.
Here is my code:

    public class QueryServer extends AsyncTask <String, Void, Integer> {       protected Integer doInBackground(String... serverAddress) {         Log.d("QueryServer", ""+serverAddress[0]);         MCQuery mcQuery = new MCQuery("" + serverAddress[0] ,25565);         QueryResponse response = mcQuery.basicStat();          int Onlineplayers = response.getOnlinePlayers(); //first vaule         int Maxplayers = response.getMaxPlayers();  //second vaule          Log.d("MCQuery", "" + Onlineplayers + " OnlinePlayers");         return Onlineplayers;      }      protected void onPostExecute(Integer Onlineplayers){          TextView onlinePlayersView = (TextView) findViewById(R.id.online_players);          onlinePlayersView.setText(""+Onlineplayers+"/"+ Maxplayers); //i need to pass Maxplayers to use it here       } 

Thank you in advance.

like image 753
Boris Avatar asked Aug 06 '12 18:08

Boris


People also ask

How many times an instance of AsyncTask can be executed?

AsyncTask instances can only be used one time.

What is AsyncTask with a neat diagram explain the steps for execution of AsyncTask?

Android AsyncTask going to do background operation on background thread and update on main thread. In android we cant directly touch background thread to main thread in android development. asynctask help us to make communication between background thread to main thread.

What happen if we call execute more than once in AsyncTask?

Limitation Of AsyncTask There is a limit of how many tasks can be run simultaneously. Since AsyncTask uses a thread pool executor with max number of worker threads (128) and the delayed tasks queue has fixed size 10. If you try to execute more than 138 AsyncTasks the app will crash with java.


2 Answers

You can define a Wrapper class that holds two integers:

public class Wrapper {     public int onlinePlayers;     public int maxPlayers; } 

and use it in place of Integer:

public class QueryServer extends AsyncTask<String, Void, Wrapper> {       protected Wrapper doInBackground(String... serverAddress) {         Log.d("QueryServer", ""+serverAddress[0]);         MCQuery mcQuery = new MCQuery("" + serverAddress[0] ,25565);         QueryResponse response = mcQuery.basicStat();          int onlinePlayers = response.getOnlinePlayers(); //first vaule         int maxPlayers = response.getMaxPlayers();  //second vaule          Log.d("MCQuery", "" + onlinePlayers + " onlinePlayers");         Wrapper w = new Wrapper();         w.onlinePlayers = onlinePlayers;         w.maxPlayers = maxPlayers;         return w;      }      protected void onPostExecute(Wrapper w){          TextView onlinePlayersView = (TextView) findViewById(R.id.online_players);          onlinePlayersView.setText(""+w.onlinePlayers+"/"+ w.maxPlayers); //i need to pass Maxplayers to use it here       } 
like image 181
Eng.Fouad Avatar answered Sep 24 '22 15:09

Eng.Fouad


public class QueryServer extends AsyncTask <String, Void, Integer>{...} 

Replace the Integer generic param to ArrayList<Integer> or to Integer[]

This way your doInBackground() will look like this:

protected Integer[] doInBackground(String... serverAddress)  {     ...do what you need to do...     //Then create an array, fill with data, and return.     Integer[] arr = new Integer[10];     for(int i=0; i<10; i++)         arr[i] = i; //just an example     return arr; } 

And the in your onPostExecute()

protected void onPostExecute(Integer [] Onlineplayers) {     //Do whatever you want with your array } 
like image 20
Balázs Édes Avatar answered Sep 24 '22 15:09

Balázs Édes