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