我有一个FTP连接工作正常,我可以使用一个调用>的片段下载我的文件调用>的asynctaskt FTP服务器
public static void downloadDirectory(FTPClient ftpClient, String parentDir,
String currentDir, String saveDir) throws IOException {
String dirToList = parentDir;
if (!currentDir.equals("")) {
dirToList += "/" + currentDir;
}
FTPFile[] subFiles = ftpClient.listFiles(dirToList);
if (subFiles != null && subFiles.length > 0) {
for (FTPFile aFile : subFiles) {
String currentFileName = aFile.getName();
if (currentFileName.equals(".") || currentFileName.equals("..")) {
// skip parent directory and the directory itself
continue;
}
String filePath = parentDir + "/" + currentDir + "/"
+ currentFileName;
if (currentDir.equals("")) {
filePath = parentDir + "/" + currentFileName;
}
String newDirPath = saveDir + parentDir + File.separator
+ currentDir + File.separator + currentFileName;
if (currentDir.equals("")) {
newDirPath = saveDir + parentDir + File.separator
+ currentFileName;
}
if (aFile.isDirectory()) {
// create the directory in saveDir
File newDir = new File(newDirPath);
boolean created = newDir.mkdirs();
if (created) {
System.out.println("CREATED the directory: "
+ newDirPath);
} else {
System.out.println("COULD NOT create the directory: "
+ newDirPath);
}
// download the sub directory
downloadDirectory(ftpClient, dirToList, currentFileName,
saveDir);
} else {
// download the file
boolean success = downloadSingleFile(ftpClient, filePath,
newDirPath);
if (success) {
System.out.println("DOWNLOADED the file: " + filePath);
} else {
System.out.println("COULD NOT download the file: "
+ filePath);
}
}
}
try {
Log.v("LogoutInformation", "Logout from FTP");
ftpClient.logout();
} catch (Exception e) {
Log.e("LogoutInformation", "Logout Fail");
}
try {
Log.v("DisconnectInformation", "Disconnect from FTP");
ftpClient.disconnect();
} catch (Exception e) {
Log.e("DisconnectInformation", "Disconnect Fail");
}
}
这是我的FTPserver功能。
System.out.println("DOWNLOADED the file: " + filePath);
这就是我想要在我的片段上显示的内容,我想制作一个显示实际下载文件的Toast。 但是它的后台处理,所以我无法在我的片段上显示信息,所以我不知道我该怎么做。
答案 0 :(得分:1)
从标签开始,您使用的是AsyncTask。修改它的构造函数以接受对context
的引用。
public class MyAsyncTask extends AsyncTask<..> {
private Context mContext;
public MyAsyncTask(Context c){
this.mContext = c;
}
...
当您的任务在doInBackground
中运行时,您应该publishProgress(String)
:
doInBackground(String.. params){
for(something){
...
publishProgress(fileName);
}
}
publishProgress(String fName){
Toast.makeText(mContext, name, Toast.Length_long).show();
}
答案 1 :(得分:0)
尝试使用:
Toast.makeText(getActivity(), "File Downloaded:"+currentFileName, Toast.LENGTH_LONG).show()
答案 2 :(得分:0)
您是否在AsyncTask类的doInBackground事件中调用downloadDirectory?
如果是这样,您可以像这样制作Toast消息:
@Override
protected void onPostExecute(Void result) {
Toast.makeText(getActivity(),"DOWNLOADED the file: "+filePath,Toast.LENGTH_SHORT).show();
}
创建filePath全局变量的位置。
这样的事情。希望它有所帮助..
答案 3 :(得分:0)
你只需要这样做。
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context,"Your text", Toast.LENGTH_LONG).show();
}
});