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方法中,我在这次讨论中找到了解决方案enter link description here
答案 0 :(得分:1)
为了阻止你做的事情:
首先 - 从你的主线程:
conx.cancel(true);
你的doInBackground方法中的和第二个(String params ..)
if(this.isCancelled()){
return "interrupt"
}
答案 1 :(得分:1)
您可以使用task.cancel(true);
,但通常情况下,如果您的doInBackground()
中有一个循环,并检查其中isCancelled
的值。但没有循环的情况。
答案 2 :(得分:0)
task.cancel(true);
停止AsyncTask
答案 3 :(得分: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");