How to list files and folders on ftp server?

1

I'm not able to make the FTP server listing in the app using the Android emulator, I realize that in older versions of Android the listing usually happens.

But from Android 4 I have problems for this, I also tested running the project on my mobile phone that is on Android 6 and I have same problem. Only works in older versions.

I can create folders by application normally I just can not list. The files and folders are stored in an array that are listed in a listview in an Activity folder and other files are listed.

Connection methods called

public boolean Conectar(String Host, String Usuario, String Senha, int Porta) {
    try {
        mFtp = new FTPClient();

        mFtp.connect(Host, Porta);
        if (FTPReply.isPositiveCompletion(mFtp.getReplyCode())) {
            Log.i(TAG, "Conexão foi feita");
            boolean status = mFtp.login(Usuario, Senha);

            mFtp.setFileType(FTP.BINARY_FILE_TYPE);
            mFtp.enterLocalPassiveMode();
            Log.i("STATUS", "CONECTADO");
            return status;
        }
    } catch (Exception e) {
        Log.e(TAG, "Erro: não foi possível conectar " + Host);
    }
    return false;
}

Method called list of files and folders

public FTPFile[] Dir(String Diretorio) {
    try {
        FTPFile[] ftpFiles = mFtp.listFiles(Diretorio);
        return ftpFiles;
    } catch (Exception e) {
        Log.e(TAG,
                "Erro: não foi possível  listar os   arquivos do diretório "
                        + Diretorio + ". " + e.getMessage());
    }

    return null;
}

public FTPFile[] DirPasta(String Diretorio) {
    try {
        FTPFile[] ftpFiles = mFtp.listDirectories(Diretorio);
        return ftpFiles;
    } catch (Exception e) {
        Log.e(TAG, "Erro: não foi possível  listar pastas do diretório "
                + Diretorio + ". " + e.getMessage());
    }

    return null;
}

Method that lists folders

    public void ListarPastasdoFTP() {
    Log.i(TESTE, "Entrou no ListarArquivosdoFTP");
    classe_FTP ClienteFTP = new classe_FTP();
    ClienteFTP.Conectar("31.170.165.237", "meuUsuario", "123", 21);

    FTPFile[] arquivos = ClienteFTP.DirPasta("/public_html/uploads");
    if (arquivos != null) {
        int length = arquivos.length;
        for (int i = 0; i < length; ++i) {
            FTPFile f = arquivos[i];
            if (f.isDirectory()) {
                Log.i(TESTE, "Entrou no ListarArquivosdoFTP 2");
                ArquivosFTP.add(f.getName());
            }
        }
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, ArquivosFTP);
        LvlistarArquivosFTP.setAdapter(arrayAdapter);

        arquivosArray = ArquivosFTP;

        Log.i(TESTE, "Entrou no ListarArquivosdoFTP 3, ArquivosArray: "
                + arquivosArray);
    }

}

Method that lists files

public void ListarArquivosdoFTP() {
    // Log.i(TESTE, "Entrou no ListarArquivosdoFTP pasta: "+ pasta);
    classe_FTP ClienteFTP = new classe_FTP();
    ClienteFTP.Conectar("31.170.165.237", "meuUsuario", "123", 21);

    // MainActivity at = new MainActivity();
    // String pasta = at.nomePasta;
    Log.i(TESTE, "Entrou no ListarArquivosdoFTP 1");

    String caminho = "/public_html/uploads";
    String caminhoCompleto = caminho + File.separator + itemClicado;

    Log.i(TESTE, "Caminho Completo: " + caminhoCompleto);

    FTPFile[] arquivos = ClienteFTP.Dir(caminhoCompleto);
    if (arquivos != null) {
        int length = arquivos.length;
        for (int i = 0; i < length; ++i) {
            FTPFile f = arquivos[i];
            if (f.isFile()) {
                Log.i(TESTE, "Entrou no ListarArquivosdoFTP 2");
                ArquivosFTP.add(f.getName());
            }
        }
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, ArquivosFTP);
        LvlistarArquivosFTP.setAdapter(arrayAdapter);
        arquivosArray = ArquivosFTP;
    }

}

People find out why it is not listing, The problem was trying to insert an information in the Thread of the graphical interface inside another Thread, escaping from the current scope.

And the part that does not execute is exactly what I inserted into the array to display in the graphical interface.

            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, ArquivosFTP);
        LvlistarArquivosFTP.setAdapter(arrayAdapter);
        arquivosArray = ArquivosFTP;

I'm wondering how to insert this Array files into the Thread to execute in the graphical interface in the case in the listview.

In your opinion what is the best class to solve this Handler, AsyncTask or other class problem? If so, how? Using the Handle and trying to pass the information to the Thread the files of the array got there null because it did not execute the ArrayAdapter. How to resolve in this context?

Any information, I am available.

    
asked by anonymous 22.10.2016 / 02:05

1 answer

2

I discovered that I had problems with Thread, where only the Main Thread can change the views and to change views for another Thread needs access to the Main Thread through the Handler. I was trying to add items to the layout through the setAdapter by another Thread. Type:

                    handler.post(new Runnable() {

                    @Override
                    public void run() {

                   //Código para alterar Layout, acessando Thread Principal

                                    });

                    }



                 new Thread() {
                 public void run() {

                 //Código para conexão ao servidor (Por Exemplo), acessando outra Thread

                     }
                 }.start();
    
04.11.2016 / 14:28