Android ganymed ssh2

时间:2015-02-25 19:30:15

标签: java android ssh

我尝试打开与服务器的ssh连接。为此,我使用Ganymed SSH2修订版262。

我创建了一个ssh java类

public class SSH {

public String connection() {

    String Hostname = "XX.XX.XX.XX";
    String Login = "XXXXX";
    String PWD = "*******";
    String result = "";

    try {
        Connection connection = new Connection(Hostname);
        connection.connect();

        boolean isAuthenticated = connection.authenticateWithPassword(Login, PWD);

        if (isAuthenticated == false)
            throw new IOException("Authentication failed.");

        Session session = connection.openSession();

        session.execCommand("uname -a");


        InputStream stream = new StreamGobbler(session.getStdout());
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));

        while (true)
        {
            String line = bufferedReader.readLine();
            if (line == null)
                break;
            result = line;
        }


        session.close();

        connection.close();

    }
    catch (IOException e)
    {
        e.printStackTrace(System.err);
        System.exit(2);
    }

    return result;
}}

我按下按钮

尝试连接我的活动
public void sshConnect(View view) {

    SSH ssh = new SSH();
    SSHResult = ssh.connection();

}

但我收到了错误

java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:4026)
        at android.view.View.performClick(View.java:4785)
        at android.view.View$PerformClick.run(View.java:19858)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5696)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at android.view.View$1.onClick(View.java:4021)
        at android.view.View.performClick(View.java:4785)
        at android.view.View$PerformClick.run(View.java:19858)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5696)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
 Caused by: android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1155)
        at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
        at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
        at libcore.io.IoBridge.connect(IoBridge.java:122)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
        at java.net.Socket.connect(Socket.java:882)
        at ch.ethz.ssh2.transport.ClientTransportManager.connect(ClientTransportManager.java:62)
        at ch.ethz.ssh2.transport.ClientTransportManager.connect(ClientTransportManager.java:34)
        at ch.ethz.ssh2.Connection.connect(Connection.java:754)
        at ch.ethz.ssh2.Connection.connect(Connection.java:628)
        at info.senyk.ustat.SSH.connection(SSH.java:28)
        at info.senyk.ustat.StatsActivity.sshConnect(StatsActivity.java:118)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at android.view.View$1.onClick(View.java:4021)
        at android.view.View.performClick(View.java:4785)
        at android.view.View$PerformClick.run(View.java:19858)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5696)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

帮助我在哪里犯了错误?

1 个答案:

答案 0 :(得分:0)

您的错误是由上一个Caused by

引起的
Caused by: android.os.NetworkOnMainThreadException

尝试在主线程上执行网络操作时抛出此异常。要解决此问题,您可以使用AsyncTask类作为后台操作执行连接。