Skip to content Skip to sidebar Skip to footer

How To Get List Of Files From Sftp Server In Android?

I developed a code but it doesn't work.please help me. ChannelSftp sftpChannel = (ChannelSftp) channel; try { Vector ls=sftpChannel.ls('/home/')

Solution 1:

You cannot modify the UI (t.setText()) from a background thread: e.g you can use post() method to execute a runnable in the UI thread if you are currently in a background thread.

Moreover in your code you are replacing the text of the field instead of appending a new line.

Try with the following code:

            ChannelSftp sftpChannel = (ChannelSftp) channel;
            try {
                Vector ls=sftpChannel.ls("/home/");
                String text = "";
                for(int i=0;i<ls.size();i++){
                    text += ls.get(i)+"\n";
                }
                t.post(new Runnable() {
                    publicvoidrun() {
                       t.setText(text);
                    }
                });
            } catch (SftpException e1) {
                // TODO Auto-generated catch block
            }

It would actually be better to display the results with a ListView. A quick (& dirty) example:

        listView.setAdapter(newBaseAdapter() {

        @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
            TextViewview=newTextView(parent.getContext());
            view.setText(((LsEntry)ls.get(i)).getFilename());
            return view;
        }

        @OverridepublicintgetCount() {
            return ls.size();
        }

        @OverridepubliclonggetItemId(int position) {
            return0;
        }

        @Overridepublic Object getItem(int position) {
            returnnull;
        }
    });

Solution 2:

if you want to list just the file name:

newThread(newRunnable() {
            @Overridepublicvoidrun() {
                Sessionsession= startSession();
                Stringlocal="/home/";
                Stringremote="/tmp/";
                Channelchannel=null;
                try {
                    channel = session.openChannel("sftp");
                    channel.connect();
                    ChannelSftpsftp= (ChannelSftp) channel;
                    Vector<ChannelSftp.LsEntry> list = sftp.ls(remote);
                    Stringfile="";
                    for(ChannelSftp.LsEntry entry : list) {
                        file = entry.getFilename();
                        // This is just the name of the file
                    }
                } catch (SftpException e1) {

                } catch (JSchException e) {
                    e.printStackTrace();
                } finally {
                    if (channel != null) {
                       channel.disconnect();
                    }
                }
            }
                endSession(session);
                // You need to handle start and end session.
            }
        }).start();
    }

Post a Comment for "How To Get List Of Files From Sftp Server In Android?"