How to return more than one value using an asyncTask in android?

4

In short, my class that extends an AsyncTask, downloads a series of data of type byte, then I need to perform some calculations with that data, and finally return all these values to my MainActivity.

The code is summarized as follows:

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

So, I would like to know how I can get other values of different types computed in this same AsyncTask in my MainActivity class, in addition to the "data" byte array. Thanks!

    
asked by anonymous 22.06.2015 / 13:32

1 answer

2

A method only returns a data type, however, this type does not necessarily need to be a simple type such as int , String , boolean , etc.
So, when you want to return a dataset, a class is used.

Type a class that has the attributes 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 AsyncTask so that the type that the doInBackground() method returns is this class:

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

    .....
    .....
}  

The doInBackground() method 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 downLoadData() method, create an object of the type by passing the constructor byte[] and 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 onPostExecute() fault method, you receive this dataset:

@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();
    }
}  
    
22.06.2015 / 16:00