How to get list of Files from SFTP Server in android?

自闭症网瘾萝莉.ら 提交于 2019-12-12 04:36:13

问题


I developed a code but it doesn't work.please help me.

ChannelSftp sftpChannel = (ChannelSftp) channel;
                try {
                    Vector ls=sftpChannel.ls("/home/");
                    for(int i=0;i<ls.size();i++){

                    t.setText("\n"+ls.get(i)+"\n");


                    }

                } catch (SftpException e1) {
                    // TODO Auto-generated catch block
                }

Here is the Exception:

11-06 12:29:15.801: E/AndroidRuntime(9624): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-06 12:29:15.801: E/AndroidRuntime(9624):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4267)
11-06 12:29:15.801: E/AndroidRuntime(9624):     at android.view.ViewRootImpl.invalidateChild(ViewRootImpl.java:855)
11-06 12:29:15.801: E/AndroidRuntime(9624):     at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:904)
11-06 12:29:15.801: E/AndroidRuntime(9624):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:4005)
11-06 12:29:15.801: E/AndroidRuntime(9624):     at android.view.View.invalidate(View.java:8716)
11-06 12:29:15.801: E/AndroidRuntime(9624):     at android.view.View.invalidate(View.java:8667)
11-06 12:29:15.801: E/AndroidRuntime(9624):     at android.widget.TextView.updateAfterEdit(TextView.java:6265)
11-06 12:29:15.801: E/AndroidRuntime(9624):     at android.widget.TextView.handleTextChanged(TextView.java:7909)
11-06 12:29:15.801: E/AndroidRuntime(9624):     at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8286)
11-06 12:29:15.801: E/AndroidRuntime(9624):     at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:892)
11-06 12:29:15.801: E/AndroidRuntime(9624):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
11-06 12:29:15.801: E/AndroidRuntime(9624):     at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:266)
11-06 12:29:15.801: E/AndroidRuntime(9624):     at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:443)
11-06 12:29:15.801: E/AndroidRuntime(9624):     at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:257)
11-06 12:29:15.801: E/AndroidRuntime(9624):     at android.text.SpannableStringBuilder.append(SpannableStringBuilder.java:29)
11-06 12:29:41.941: W/IInputConnectionWrapper(9624): showStatusIcon on inactive InputConnection

回答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() {
                    public void run() {
                       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(new BaseAdapter() {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView view = new TextView(parent.getContext());
            view.setText(((LsEntry)ls.get(i)).getFilename());
            return view;
        }

        @Override
        public int getCount() {
            return ls.size();
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }
    });



回答2:


if you want to list just the file name:

    new Thread(new Runnable() {
            @Override
            public void run() {
                Session session = startSession();
                String local = "/home/";
                String remote = "/tmp/";
                Channel channel = null;
                try {
                    channel = session.openChannel("sftp");
                    channel.connect();
                    ChannelSftp sftp = (ChannelSftp) channel;
                    Vector<ChannelSftp.LsEntry> list = sftp.ls(remote);
                    String file = "";
                    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();
    }


来源:https://stackoverflow.com/questions/13249143/how-to-get-list-of-files-from-sftp-server-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!