【发布时间】:2011-10-12 14:54:02
【问题描述】:
我首先想获取存储在 FTP 目录中的文件列表,然后使用时间戳获取最后创建的文件的名称。我收到一个警告框:Activity is not responding。检查 logcat 条目后,我注意到代码永远不会到达行:
Log.e("FTP", "number of filenames: " + count);
但我到达Log.e("FTP", "Connexion successful "); 所以连接到服务器似乎没问题。
那里似乎出了点问题。有人可以帮我处理它。或者告诉我一个从 FTP 服务器导向器获取最后创建的文件的简单方法?
FTPClient ftpClient = new FTPClient();
try
{
ftpClient.connect(InetAddress.getByName(Fonctions.address), Integer.parseInt(Fonctions.port));
if (ftpClient.login(Fonctions.login, Fonctions.pass))
{
Log.e("FTP", "Connexion successful ");
String workDir = ftpClient.printWorkingDirectory();
//Log.e("FTP", "workdir:" + workDir);
int count = ftpClient.listNames().length;
Log.e("FTP", "number of filenames: " + count);
FTPFile [] dossier = new FTPFile[count];
FTPFile back = new FTPFile();
dossier = ftpClient.listDirectories("Sarelo_FTP");
back = dossier[0];
Log.e("FTP", "Avant boucle " + back);
int buf = 0;
for (int i=0;i<(dossier.length) - 1;i++)
{
for (int j=1;j<dossier.length;j++)
{
buf = back.getTimestamp().compareTo(dossier[j].getTimestamp());
if (buf == -1)
back = dossier[j];
}
}
Log.e("FTP", "fichier final le plus récent: " + back.getName());
}
else{
Log.e("Restore FTP", "Error while connecting to FTP server");
}
}
catch(IOException e)
{
String title = "Error connecting to FTP server";
String msg = "Please check your parameters and connexion info: login, password,port number";
f.alert(c, title, msg).show();
Log.e("Restore FTP", "Error while connecting to FTP server", e);
}
P.S:我无法获取目录中的文件列表,所以我不知道检索最后创建的文件的代码是否正常工作。对此的任何帮助也将不胜感激。
[编辑] 这是我的 AsyncTask,用于检索目录中的文件列表。但它仍然无法正常工作。我不再收到 Application Not Responding 了,但它似乎没有做任何其他事情。执行卡在同一点(无法到达Log.e("FTP", "number of filenames: " + count);)
class getFilesFromFtp extends AsyncTask
{
@Override
protected String doInBackground(Object... params)
{
int count = 0;
try
{
Log.e("FTP", "avant names: " + count);
count = ftpClient.listNames().length;
Log.e("FTP", "names: " + count);
handler.sendMessage(handler.obtainMessage());
}
catch (IOException e) {
Log.e("FTP", "Error getting number of files ", e);
}
return null;
}
}
感谢您的帮助。
【问题讨论】: