如何从Android中的SFTP服务器获取文件列表?

时间:2012-11-06 10:33:44

标签: android sftp jsch

我开发了一个代码,但它不起作用。请帮助我。

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
                }

以下是例外:

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

2 个答案:

答案 0 :(得分:0)

您无法从后台线程修改UI(t.setText()):例如,如果您当前位于后台线程中,则可以使用post()方法在UI线程中执行runnable。

此外,在您的代码中,您将替换字段的文本而不是添加新行。

尝试使用以下代码:

            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
            }

使用ListView显示结果实际上会更好。一个快速(和肮脏)的例子:

        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;
        }
    });

答案 1 :(得分:0)

如果您只想列出文件名:

    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();
    }