0

in fact I have a problem in my program.
I normally have to download files from an FTP server, and I have a button to do that when I click I have to download the file.
the problem is that when I click several times. the task will not run, because I can not manage to kill asyntask. here I have put a simple example:

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

what i must add in my code to fix my bug.
thank you very much for your help

thank you very much for your help i found the solution , the problem was in the retrivefile methode , i found the solution in this discussion enter link description here

Community
  • 1
  • 1
xmen-5
  • 1,806
  • 1
  • 23
  • 44

4 Answers4

1

in order to stom you got to do :

first- from your main thread:

conx.cancel(true);

and second- in your doInBackgroundMethod(String params..)

if(this.isCancelled()){
return "interrupt"
}
Alex
  • 1,416
  • 1
  • 14
  • 24
1

You can use task.cancel(true); but usually it works if you have a loop in your doInBackground() and check the value of isCancelled in it .But in your case there is not a loop.

hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
0

task.cancel(true); to stop an AsyncTask

Simo
  • 1,200
  • 1
  • 16
  • 26
  • 1
    This is only stops it. It doesn't kill the task. – Miro Markaravanes Jun 03 '13 at 15:13
  • And there are few other things to consider when cancelling an `AsyncTask`. See the answer by @hasanghaforian: http://developer.android.com/reference/android/os/AsyncTask.html#cancel%28boolean%29 – Mike D Jun 03 '13 at 15:16
0

Instead of:

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

Just do:

// Kill any remaining tasks
conx = null;
// Start the new task
conx=new Connexion();
conx.execute("73383_20130426_Tessenderlo_VBR_3.pdf");
Miro Markaravanes
  • 3,285
  • 25
  • 32
  • sorry i used but it kill not the asyntask, and in several clik i have no answer from the program – xmen-5 Jun 04 '13 at 08:41