Error while trying to connect to Android FTP server

Asked

Viewed 233 times

-1

I am trying to connect to the external FTP server with the current lib 3.6 of apache ftp client using Android Studio, the version of my Android is 4.3 up to the current 9.

In my Manifest I have:

I use the Ftpclient with the commons-net-3.6.jar Imported in the project Lib.

In my class of connection I have.

Dir method of directory:

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 e pastas do diretório " + Diretorio + ". " + e.getMessage());
        }
        return null;
    }

The Connect method:

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

            if (FTPReply.isPositiveCompletion(mFtp.getReplyCode())) {

                boolean status = mFtp.login(Usuario, Senha);
                mFtp.setFileType(FTP.BINARY_FILE_TYPE);
                mFtp.enterLocalPassiveMode();

                return status;
            }
        } catch (Exception e) {
            Log.e(TAG, "Erro: não foi possível conectar: " + Host);
        }
        return false;
    }

And in the fragment have the methods.

public void ListarArquivosdoFTP() {
        classe_FTP ClienteFTP = new classe_FTP();
        ClienteFTP.Conectar("ftp.meuhost.com.br", "meu user", "minha senha", 21);
        FTPFile[] arquivos = ClienteFTP.Dir("/httpdocs/expresso1002/recibo/112019");
        if (arquivos != null) {
            int length = arquivos.length;
            for (int i = 0; i < length; ++i) {
                FTPFile f = arquivos[i];
                if (f.isFile()) {
                    ArquivosFTP.add(f.getName());
                }
            }
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_dropdown_item_1line, ArquivosFTP);
            SpnListarArquivosFTP.setAdapter(arrayAdapter);
        }
    }

I’m calling it that in the OnCreateView:

ListarArquivosdoFTP();

And Logcat shows this error when I open the fragment:

java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
        at com.android.server.am.ActivityManagerService.handleIncomingUser(ActivityManagerService.java:13292)
        at android.app.ActivityManager.handleIncomingUser(ActivityManager.java:2058)
        at com.android.providers.settings.SettingsProvider.callFromPackage(SettingsProvider.java:615)
        at android.content.ContentProvider$Transport.call(ContentProvider.java:279)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:273)
        at android.os.Binder.execTransact(Binder.java:388)
        at dalvik.system.NativeStart.run(Native Method)
12-16 14:30:39.461 11615-11615/expresso.escala1002 E/classeFTP: Erro: não foi possível conectar: ftp.meuhost.com.br
12-16 14:30:39.461 11615-11615/expresso.escala1002 E/classeFTP: Erro: não foi possível listar os arquivos e pastas do diretório /httpdocs/expresso1002/recibo/112019. Connection is not open

I have another method that lists a phone directory and shows all the items in one Spinner and works perfectly, I’m wanting to do the same with this, I have to access the folder indicated on FTP and list all items. How to resolve this error while trying to connect?

1 answer

-1

I managed to solve this problem.

I changed the method ListarArquivosdoFTP() adding thread:

public void ListarArquivosdoFTP() {

        new Thread(){
            @Override
            public void run(){
                try{
                    classe_FTP ClienteFTP = new classe_FTP();
                    ClienteFTP.Conectar("ftp.meuhost.com.br", "usuario", "senha", 21);
                    FTPFile[] arquivos = ClienteFTP.Dir("/caminho");
                    if (arquivos != null) {
                        int length = arquivos.length;
                        for (int i = 0; i < length; ++i) {
                            FTPFile f = arquivos[i];
                            if (f.isFile()) {
                                ArquivosFTP.add(f.getName());
                            }
                        }

                        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, ArquivosFTP);
                        SpnListarArquivosFTP.setAdapter(arrayAdapter);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
    }

Browser other questions tagged

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