其實我的程序有問題。
我通常必須從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梅索德,我發現enter link description here
看看[這個答案](http://stackoverflow.com/a/10882600/1289716) – MAC