事实上我的程序有问题。
我通常必须从 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 方法中,我在此讨论中找到了解决方案在此处输入链接描述