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?