2017-11-22 3 views
-1

私はリモートのLinuxサーバーでコマンドを実行するためにJSCHライブラリを使用してアンドロイドアプリを構築して、その出力を読み取ります。Androidでjschコマンドの出力を読み取る方法は?

回答はthis questionでしたが、私の問題は解決しませんでした。

次は、私の活動のコードです:だから

package com.example.pic_controller_2; 

     import android.graphics.drawable.Drawable; 
     import android.support.v7.app.AppCompatActivity; 
     import android.os.Bundle; 
     import android.app.Activity; 
     import java.io.BufferedInputStream; 
     import java.io.BufferedOutputStream; 
     import java.io.ByteArrayOutputStream; 
     import java.io.File; 
     import java.io.FileOutputStream; 
     import java.io.OutputStream; 
     import com.jcraft.jsch.Channel; 
     import com.jcraft.jsch.ChannelExec; 
     import com.jcraft.jsch.JSch; 
     import com.jcraft.jsch.JSchException; 
     import com.jcraft.jsch.Session; 
     import com.jcraft.jsch.ChannelSftp; 
     import android.view.View; 
     import android.os.AsyncTask; 
     import android.widget.Button; 
     import android.widget.ImageView; 
     import android.widget.Toast; 

public class TestActivity extends AppCompatActivity { 

    private String aaaa; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_test); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    } 

    public void show_picture_func (View v) { 

     new AsyncTask<Integer, Void, Void>(){  

      @Override 
      protected Void doInBackground(Integer... params) { 

       String SFTPHOST = "192.168.0.1"; 
       int SFTPPORT = 22; 
       String SFTPUSER = "pi"; 
       String SFTPPASS = "raspberry"; 
       String SFTPWORKINGDIR = "/home/pi/pic/"; 

       Session session = null; 
       Channel channel = null; 
       ChannelSftp channelSftp = null; 

       try { 
        JSch jsch = new JSch(); 
        session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT); 
        session.setPassword(SFTPPASS); 
        session.setConfig("StrictHostKeyChecking", "no"); 
        session.setTimeout(10000); 
        session.connect(); 

        ChannelExec channel1 = (ChannelExec)session.openChannel("exec"); 
        final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        channel1.setOutputStream(baos); 
        channel1.setCommand("ls /home/pi/pic | sed -n 5p"); 
        aaaa = new String(baos.toByteArray()); 

       } catch (Exception ex) { 
        ex.printStackTrace(); 
        Toast.makeText(getApplicationContext(), "Error Connecting", Toast.LENGTH_LONG).show(); 
       } 
       return null; 
      } 


      @Override 
      protected void onPostExecute(Void unused) { 
       TextView textView1_2 = (TextView)findViewById(R.id.textView1); 
       textView1_2.setText(aaaa); 
      } 


     }.execute(1); 

    } 

} 

、実行したコマンドの出力は、TextViewのに表示されます。しかし、それはしません。テキストビューが白に変わります!

何か提案がありますか?

+1

最初の明らかな問題は、 'channel1.connect()'を決して呼び出さないということです。そのため、コマンドは決して実行されません。 2番目の問題は 'toByteArray'を呼び出す前にコマンドが完了するのを待たなければならないことです。 –

+0

あなたのコメントのために@MartinPrikrylに感謝します。私はこの問題を早期に認識しました。私の愚かなこと!!私が投稿した別のLinuxコマンドの問題がありました。 – Omid1989

答えて

0

私はこのことから、コードの次のセクションを変更:これに

ChannelExec channel1 = (ChannelExec)session.openChannel("exec"); 
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
channel1.setOutputStream(baos); 
channel1.setCommand("ls /home/pi/pic | sed -n 5p"); 
aaaa = new String(baos.toByteArray()); 

ChannelExec channel1 = (ChannelExec)session.openChannel("exec"); 
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
channel1.setOutputStream(baos); 
channel1.setCommand("ls /home/pi/pic | sed -n 5p | xargs echo -n"); 
channel1.connect(); 
try{Thread.sleep(1000);}catch(Exception ee){} 
aaaa = new String(baos.toByteArray()); 

、問題解決。

関連する問題