如何在Android

时间:2017-07-12 07:34:59

标签: java android android-asynctask

我想在分离的类中创建我的AsyncTask,因为我必须解析XML并且它是一堆代码。

因此,每当asynctask完成时,我都希望在活动中注意到。

我关注this question

问题在于,当我必须进行多次请愿时,每次完成一次请求调用相同的方法。

我想根据我调用的AsyncTask类调用不同的方法。

编辑:我的一个AsyncTask类

public class FichaProducto extends AsyncTask<Void,Void,String>{
private String codigo, descripcion, ubicacion, descPromocion, currentUser,ip,port,codigoBuscar, respuesta;
private float precio;
private static final String TAG = "Logger";
private OnTaskCompleted listener;

/**
 * Without listener
 * @param codigoBuscar
 * @param currentUser
 * @param ip
 * @param port
 */
public FichaProducto(String codigoBuscar,String currentUser,String ip,String port) {
    setCodigoBuscar(codigoBuscar);
    setCurrentUser(currentUser);
    setIp(ip);
    setPort(port);
}
/**
 * With listener
 * @param codigoBuscar
 * @param currentUser
 * @param ip
 * @param port
 * @param listener
 */
public FichaProducto(String codigoBuscar, String currentUser, String ip, String port, OnTaskCompleted listener) {
    setCodigoBuscar(codigoBuscar);
    setCurrentUser(currentUser);
    setIp(ip);
    setPort(port);
    this.listener = listener;
}

/**
 * set the xml response
 * @param response
 */
public void setRespuesta(String respuesta) {
    this.respuesta = respuesta;
}

/**
 * @return server xml response
 */
@Override
protected String doInBackground(Void... params) {
    StringBuilder respuesta = new StringBuilder();
    URL url;

    HttpURLConnection conexion = null;

    try{
        //Create the connection and set parameters
        url = new URL("http://"+getIp()+":"+getPort());
        Log.d(TAG,url.toString());
        conexion = (HttpURLConnection)url.openConnection();
        conexion.setRequestMethod("POST");
        conexion.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conexion.setRequestProperty("Content-Length", "" + Integer.toString(getXML().getBytes().length));
        conexion.setRequestProperty("Content-Language", "es-ES");
        conexion.setUseCaches(false);
        conexion.setDoInput(true);
        conexion.setDoOutput(true);

        //Send the petition
        DataOutputStream dos = null;
        dos = new DataOutputStream(conexion.getOutputStream());
        dos.writeBytes(getXML());
        dos.flush();
        dos.close();

        //Get the response
        InputStream is = conexion.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String linea;
        while ((linea = br.readLine()) != null){
            respuesta.append(linea);
            respuesta.append("\n");
        }
        br.close();
        Log.d(TAG,"From asynctask the response is: "+respuesta.toString());
        return respuesta.toString();


    }catch(MalformedURLException e){
        Log.e(TAG,"MalformedURLException in AsyncTask "+e.getMessage());
        return null;
    }catch (IOException e){
        Log.e(TAG,"IO Exception in AsyncTask "+e.getMessage());
        return null;
    } finally {
        //Close the connection
        if (conexion != null){
            conexion.disconnect();
        }
    }
}

/**
 * Set the response and call the listener
 */
@Override
protected void onPostExecute(String respuesta){
    setRespuesta(respuesta);
    //Here it'll read the xml received and will set the variables
    if (listener != null){
        listener.onTaskCompleted();
    }
}

(如果不好,请原谅我的英语)

1 个答案:

答案 0 :(得分:0)

要检查哪个任务是强制的,您可以使用以下命令: 使用OnTaskCompleted

更新onTaskCompleted(int id);
private OnTaskCompleted listener;
private int id;

public FichaProducto(String codigoBuscar, String currentUser, String ip, 
    String port, OnTaskCompleted listener, int id) {
    setCodigoBuscar(codigoBuscar);
    setCurrentUser(currentUser);
    setIp(ip);
    setPort(port);
    this.listener = listener;
    this.id = id
}

@Override
protected void onPostExecute(String respuesta){
    setRespuesta(respuesta);
    //Here it'll read the xml received and will set the variables
    if (listener != null){
        listener.onTaskCompleted(id);
    }
}

在活动课程中,您可以使用以下内容:

private final int PARSE_XML = 0
private final int PARSE_JSON = 1

void parseXml(){
  new FichaProducto(codigoBuscar, currentUser, ip, port, PARSE_XML)
}

@Override
onTaskCompleted(int id){
    switch (id){
      case PARSE_XML:
       break;
      case PARSE_JSON:
       break;
    }
}