How to return more than one value using an Asynctask on android?

Asked

Viewed 572 times

4

Briefly, my class that extends an Asynctask, downloads a series of data byte type, then I need to perform some calculations with this data, and finally return all these values to my Mainactivity.

The code boils down to:

class DataDownloaderTask extends AsyncTask<String, Void, byte[]> {

private String url;
private Context context;
private static byte[] data;

//--------------------CONSTRUCTOR--------------------------------//
public DataDownloaderTask(Context context) {
    this.data = null;
    this.context = context;
}

//--------------BACKGROUND---------------------------------------//
@Override
// Actual download method, run in the task thread
protected byte[] doInBackground(String... params) {
    // params comes from the execute() call: params[0] is the url.
    return downloadData(params[0]);
}

//--------------POST EXECUTE-------------------------------------//
@Override
// Once the image is downloaded
protected void onPostExecute(byte[] data) {
    if(data != null){
        Toast.makeText(this.context,"Operação concluida com sucesso!", Toast.LENGTH_SHORT).show();
    }else{
        pDialog.dismiss();
        Toast.makeText(this.context,"O ficheiro nao existe ou ocorreu um erro de conexão!", Toast.LENGTH_SHORT).show();
    }
}

the method that downloads the data:

static Bitmap downloadData(String url) {

   //Download the data from url...

   //Calculate other values...
   int value1 = 0;
   int value2 = 1;
   double value 3 = 0.1;

   //COMO RETORNAR OS DADOS: value1 value2 value 3 ...?

   return data;
}

in my mainactivity I have a method that does this:

DataDownloaderTask object = new DataDownloaderTask(MainActivity.this);
object.execute(string);

    try {
        mydata = object.get(); //mydata é to tipo byte[]
    } catch (Exception e) {
        return false;
    }

Therefore, I would like to know how I can obtain in my Mainactivity class, in addition to the "data" byte array, other values of different types calculated in that same Asynctask. Thank you!

1 answer

2


A method only returns one type of data, however, this type need not necessarily be a type simple as int, String, Boolean, etc..
So when you want to return a data set, you use a class.

Write a class that has as attributes the data you want to return:

public class MeusDados{
    private int mValue1;
    private int mValue2;
    private double mValue3;
    private byte[] mData;

    public MeusDados(int value1, int value2, double value3, byte[] data){
        mValue1 = value1;
        mValue2 = value2;
        mValue3 = value3;
        mData = data;
    }

    public int getValue1(){
        return mValue1;
    }

    public int getValue2(){
        return mValue2;
    }
    public double getValue3(){
        return mValue3;
    }
    public byte[] getData(){
        return mData;
    }
}

Declare the Asynctask so that the type of the method doInBackground() return be that class:

class DataDownloaderTask extends AsyncTask<String, Void, MeusDados> {

    .....
    .....
}  

The method doInBackground() is declared as follows:

@Override
// Actual download method, run in the task thread
protected MeusDados doInBackground(String... params) {
    // params comes from the execute() call: params[0] is the url.
    return downloadData(params[0]);
}

In the method downLoadData() create an object of type Meusdados passing to the builder the byte[] and the calculated data:

static MeusDados downloadData(String url) {

   //Download the data from url...
   byte[] data = //ler do url

   //Calculate other values...
   int value1 = 0;
   int value2 = 1;
   double value3 = 0.1;

   //Criar objecto MeusDados
   MeusDados dados = new MeusDados(value1, value2, value3, data);
   //Retornar o conjunto de dados
   return dados;
}

In the method onPostExecute() receive this data set:

@Override
// Once the image is downloaded
protected void onPostExecute(MeusDados dados) {
    if(dados.getData != null){
        Toast.makeText(this.context,"Operação concluida com sucesso!", Toast.LENGTH_SHORT).show();
    }else{
        pDialog.dismiss();
        Toast.makeText(this.context,"O ficheiro nao existe ou ocorreu um erro de conexão!", Toast.LENGTH_SHORT).show();
    }
}  
  • Thank you very much :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.