How to return a data list of an asyncTask class?

1

I have an asynctask class in which I consult the data of a person in a webservice. How can you return that data to the class from which it was invoked. I have tried creating an interface class and implementing it in the asynctask, but it returns as null. This is my interface

public interface IConductor {
void asignaConductor(ArrayList <String> condutor);
public ArrayList <String> getConductor();

}

My asyntask is this way

@Override
    protected ArrayList<String> doInBackground(String... strings) {
        Log.i("ConsultaChofer", "doInBackground");

        httpClient = new DefaultHttpClient();
        IMEI = strings[0];
        get = new HttpGet("http://192.168.1.68:8080/monitoreogps/Path/webservice/DatosConductor.php?IMEI=" + IMEI);
        get.setHeader("Content-type", "application/json");

        try {
            Log.i("ConsultaChofer", "Intetando consulta");

            response = httpClient.execute(get);
            respuesta = EntityUtils.toString(response.getEntity());
            respJSON = new JSONObject(respuesta);
            estado = respJSON.getString("estado");
            conductor = new ArrayList<String>();


            Log.i("ConsultaChofer", "Estado " + estado);
            if (estado.equals("1")) {
                noEconomico = ( respJSON.getString("Num_Economico"));
                nombreC = ( respJSON.getString("NombreCompleto"));
                telefono = (respJSON.getString("Telefono"));
                licencia = (respJSON.getString("Licencia"));
                domicilio = (respJSON.getString("Domicilio"));
                conductor.add(noEconomico);
                conductor.add(nombreC);
                conductor.add(telefono);
                conductor.add(licencia);
                conductor.add(domicilio);
                conductor.add(estado);
                asignaConductor(conductor);
            } else {
                Log.i("ConsultaChofer", "No se obtuvieron datos");
            }

        } catch (Exception ex) {
            Log.e("Servicio Rest", "Error", ex);
            estado = "0";
            ex.printStackTrace();
        }
        return null;
    }



    @Override
    protected void onPostExecute(ArrayList<String> result) {
        Log.i("ServicioRest", "onPostExecute");
        if (estado.equals("1")) {
            //asignaConductor();
            Log.i("ServicioRest", "Conductor " + conductor.get(1));
        }

    }

    @Override
    protected void onPreExecute() {
        Log.i("ServicioRest", "onPreExecute");

    }


    @Override
    public void asignaConductor(ArrayList <String> conductor) {
        datos = new ArrayList<String>();
        datos = conductor;
    }

    @Override
    public ArrayList<String> getConductor() {
        return datos;
    }

And the way I run the asynctask call from the class

ObtenerConductor tarea = new ObtenerConductor();
        tarea.execute(imei);

        ArrayList<String> conductor = new ArrayList<String>();
        conductor =  tarea.getConductor();

Calling the method "getConductor" closes the application and returns null. How else could the same arrayList return from the asyncTask?

    
asked by Ken 02.08.2016 в 22:21
source

2 answers

1

Your class AsyncTask is GetConductor, I tell you some tips:

first your method doInBackground() must return the ArrayList with data,

   @Override
    protected ArrayList<String> doInBackground(String... strings) {
    ...
    ...
    ...
     //return null;  //No sirve de nada retornar Null!
    return conductor;
    }

this value will be received by onPostExecute() :

 @Override
    protected void onPostExecute(ArrayList<String> conductor) {
        Log.i("ServicioRest", "onPostExecute");
        asignaConductor(conductor);

        /*if (estado.equals("1")) {
            //asignaConductor();
            Log.i("ServicioRest", "Conductor " + conductor.get(1));
        }*/

    }

Your methods would be:

 @Override
    public void asignaConductor(ArrayList <String> conductor) {
        //datos = new ArrayList<String>();
        //datos = conductor;

        //Se crea una copia del arrayList para la variable datos.
        datos = new ArrayList<String>(conductor);
    }

    @Override
    public ArrayList<String> getConductor() {
        return datos;
    }

With this you can obtain by means of the method getConductor() the value of the ArrayList data.

    
answered by 03.08.2016 в 00:50
0

You can try the following:

Implementation in the AsyncTask:

public class MyAsyncTask extends AsyncTask{

  public interface IConductor {
        void asignaConductor(ArrayList <String> condutor);
  }

  public IConductor response = null;

    public MyAsyncTask(IConductor response){
        this.response = response;
    }

    @Override
    protected void onPostExecute(ArrayList <String> condutor) {
      response.asignaConductor(condutor);
    }
}

Implementation where you run the Asynctask:

MyAsyncTask asyncTask =new MyAsyncTask(new AsyncResponse(){

     @Override
     void asignaConductor(ArrayList <String> condutor){
     //Aqui lo que quieras hacer con la respuesta.
     }
  }).execute();
    
answered by 03.08.2016 в 00:21