0

事实上我的程序有问题。
我通常必须从 FTP 服务器下载文件,当我单击时我有一个按钮可以下载文件。
问题是当我点击几次时。该任务将无法运行,因为我无法设法杀死 asyntask。这里我举了一个简单的例子:

public class MainActivity extends Activity {


Connexion conx=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button bt= (Button) findViewById(R.id.button1);
    bt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (conx!=null){
                Log.i("voila", "we are here 1");
                conx.cancel(true);
                conx=new Connexion();
                conx.execute("73383_20130426_Tessenderlo_VBR_3.pdf");

            }else {
            conx=new Connexion();
            conx.execute("73383_20130426_Tessenderlo_VBR_3.pdf");}

        }
    });
}

class Connexion extends AsyncTask<String, String, String> {
    FTPClient mFTPClient;
    @Override
    protected String doInBackground(String... params) {
        Log.i("voila", "we are here 2");
         String chaine = params[0];
             try {
                    mFTPClient = new FTPClient();
                    mFTPClient.connect("site", 21);
                    Log.i("voila", "we are here 4");
                    if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
                        boolean status = mFTPClient.login("user", "pass");
                        mFTPClient.enterLocalPassiveMode();
                        ftpDownload("/fromCIS/" +chaine ,
                                Environment.getExternalStorageDirectory()
                                        + "/Fromcis/" + chaine);
                          mFTPClient.logout();  
                          mFTPClient.disconnect();

                    }
                } catch (Exception e) {

                }
             return "zaki";    
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        Log.i("voila", "we are here onpost");
        conx=null;

    }

    public boolean ftpDownload(String srcFilePath, String desFilePath) {
        boolean status = false;
        try {
            FileOutputStream desFileStream = new FileOutputStream(
                    desFilePath);
            ;
            status = mFTPClient.retrieveFile(srcFilePath, desFileStream);
            desFileStream.close();

            return status;
        } catch (Exception e) {
            Log.d(e.getCause() + "", "download failed");
        }

        return status;
    }
}
}

我必须在我的代码中添加什么来修复我的错误。
非常感谢您的帮助

非常感谢您的帮助我找到了解决方案,问题出在 retrivefile 方法中,我在此讨论中找到了解决方案在此处输入链接描述

4

4 回答 4

1

为了 stom 你必须做:

首先 - 从你的主线程:

conx.cancel(true);

第二个在你的 doInBackgroundMethod(String params..)

if(this.isCancelled()){
return "interrupt"
}
于 2013-06-03T15:15:45.553 回答
1

你可以使用task.cancel(true);,但如果你有一个循环doInBackground()并检查它的值,isCancelled它通常会起作用。但是在您的情况下,没有循环。

于 2013-06-03T15:16:18.300 回答
0

task.cancel(true);停止 AsyncTask

于 2013-06-03T15:12:53.107 回答
0

代替:

if (conx!=null){
      Log.i("voila", "we are here 1");
      conx.cancel(true);
      conx=new Connexion();
      conx.execute("73383_20130426_Tessenderlo_VBR_3.pdf");
}else {
      conx=new Connexion();
      conx.execute("73383_20130426_Tessenderlo_VBR_3.pdf");}

做就是了:

// Kill any remaining tasks
conx = null;
// Start the new task
conx=new Connexion();
conx.execute("73383_20130426_Tessenderlo_VBR_3.pdf");
于 2013-06-03T15:17:03.293 回答