2012-11-06 120 views
0

我開發了一個代碼,但它不工作。請幫助我。如何從android中的SFTP服務器獲取文件列表?

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 

回答

0

您不能修改UI(t.setText())從後臺線程:例如,你可以使用post()方法在UI線程,如果您目前執行的可運行在後臺線程中。

此外,在您的代碼中,您正在替換字段的文本,而不是添加新行。

嘗試用下面的代碼:

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

非常感謝它的作品,我有一個問題,我怎麼能列出這些文件在一個包含路徑和名稱的列表中,我需要那個緊急的 –

+0

你可以使用[ListView](http://developer.android.com/reference/android/widget/ListView.html)。看我的編輯 – fiddler

+0

我是否從main.xml @fiddler –

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(); 
    } 
相關問題