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.
AsyncTask instances can only be used one time.
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.
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.
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 }
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With