0

我正在从 ftp 服务器下载文件。下载代码工作正常,但是下载代码屏幕没有显示任何内容后,它变黑了。即使文件保存在指定目录中,下载功能也没有返回真实值。

  public class FTPClass{
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_file_player);

                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    Intent intent = getIntent();
    dirname = intent.getStringExtra("currentDirName");


    MyFTPClient mftp = new MyFTPClient();
    createPath = mftp.getAppRootPath().concat("/"+ dirname);
    mftp.setCurrentDir(createPath);
    System.out.println(mftp.ftpChangeDirectory(createPath));

    FTPFile[] farr = mftp.ftpListAllFiles();
    System.out.println(farr.length);

    for(int i = 0; i<farr.length;i++){
        System.out.println("SRC: "+createPath+"/"+farr[i].getName());

        String src = createPath+"/"+farr[i].getName();
        System.out.println("DEST: "+"/data/data/com.example.ftpplayer" + "/app_"+dirname);
        String dest ="/data/data/com.example.ftpplayer" + "/app_"+dirname+"/"+farr[i].getName();
        System.out.println(mftp.downloadFile(src,dest));
    }
}
}

public class CallingIntent extends Activity{
        System.out.println("In item click ");
            Intent intent = new Intent(getApplicationContext(), FTPClass.class);
            String dir = ((TextView) view).getText().toString();
            intent.putExtra("currentDirName", dir);
            startActivity(intent);

}
 public class MyFTPClient{

 public boolean downloadFile(String srcPath , String destPath){

    try {
        FileOutputStream fos = new FileOutputStream(destPath);
        System.out.println(mftp.retrieveFile(srcPath, fos)); // retrieve file doesn't return true
        fos.flush();
        fos.close();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch(IOException e){
        e.printStackTrace();
    }
    return false;
}

}
4

1 回答 1

0

You need to run your code on the background thread, try using an asyncTask.

于 2013-08-16T00:06:51.203 回答