How to list files and folders from ftp server?

Asked

Viewed 1,371 times

1

I’m not able to list the FTP server in the app, using the Android emulator, I realize that in the old versions of Android the listing happens normally.

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

I can create folders through the app usually just can’t list. Files and folders are stored in an array that are listed in a listview in an Activity are listed folders and other files.

Methods called the connection

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 file and folder listing

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 they are not listing, The problem was trying to insert information into the Thread of the graphical interface within another Thread, escaping from the current scope.

And the part that doesn’t run 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;

Then I wonder how to insert these files from Array to Thread to run in the graphical interface in the case in listview.

In your opinion what is the best class to solve this problem class Handler, Asynctask or other? If yes how? Using Handle and trying to pass the information to Thread the array files arrived null because it did not run Arrayadapter. How to solve in this context?

Any information, I’m available.

1 answer

2

I ended up discovering that I was having problems in Thread, where only the main Thread can change views and to change views by another Thread needs to access the main Thread through Handler. I was trying to add items in the layout via 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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.