有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Android中管理多个异步任务

我想在单独的类中创建AsyncTask,因为我必须解析XML,而且这是一堆代码

因此,每当异步任务完成时,我都希望在活动中引起注意

我跟着this question

问题是,当我不得不提出多个请求时,每次一个请求完成时,都会调用相同的方法

我想根据我调用的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) 个答案

  1. # 1 楼答案

    要检查完成的任务,您可以使用以下命令: 用onTaskCompleted(int id);更新OnTaskCompleted

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