简单的SSH连接与JSch [英] Simple SSH connect with JSch

查看:698
本文介绍了简单的SSH连接与JSch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让从这个简单的例子东西:

I am trying to make something from this simple example :

SSH,执行远程命令与Android

我只是想看看我能不能从我的Andr​​oid手机使用SSH连接到Linux服务器,但它不工作...

I just want to see if I can connect from my android phone to a linux server using SSH but it doesn't work...

下面是我的主要code:

Here is my main code :

package com.example.ssh;

import java.util.Properties;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import android.os.Bundle;
import android.app.Activity;

 public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try
    {
        JSch jsch = new JSch();
          Session session = jsch.getSession("root","192.168.0.26", 22);
          session.setPassword("xxxxx");

          // Avoid asking for key confirmation
          Properties prop = new Properties();
          prop.put("StrictHostKeyChecking", "no");
          session.setConfig(prop);

          session.connect();

    }
    catch (Exception e)
    {
      System.out.println(e.getMessage());
    }
}
}

我做了什么错?我没有任何错误消息,我没有看到我的Linux的SSH连接。我加了库jsch和jzlib。我有没有问题得到用油灰会话连接。

What did I do wrong ? I have no error messages and I don't see any SSH connection on my Linux. I added the libraries jsch and jzlib. I have no problem to get connect with a putty session.

EDIT1:其实,我发现这解释了为什么它不工作,即使我不知道如何解决该问题的错误。错误是:

EDIT1 : In fact, I found an error which explain why it doesn't work even if I don't know how to resolve the problem. The error is :

android.os.NetworkOnMainThreadException

android.os.NetworkOnMainThreadException

所以它似乎也意味着应用程序不能在其主线程执行联网操作...

so it seems to mean that the app can't perform a networking operation on its main thread...

推荐答案

您必须执行该code在另一个线程,所以你不要挂在UI线程是什么意思例外。如果UI线程执行网络调用它不能重绘UI让用户看到一个冰冻的用户界面,并没有作出回应,而应用程序正在等待网络呼叫来完成。机器人要避免不良的用户体验,这样所以prevents你做这样的事情,抛出此异常。

You have to execute that code in another thread so you don't hang the UI thread is what that exception means. If the UI thread is executing a network call it can't repaint the UI so your users sees a frozen UI that doesn't respond to them while the app is waiting on the network call to finish. Android wants to avoid bad user experiences like this so it prevents you from doing things like this by throwing this exception.

您的onCreate()方法应该调用另一个线程(我建议使用的AsyncTask超过原纱)执行SSH连接。然后,当它完成它后的结果返回给用户界面线程和安全地从UI线程更新应用程序的用户界面。

Your onCreate() method should invoke another thread (I'd suggest using an AsyncTask over a raw thread) to perform the SSH connection. Then when it's done it can post the results back to the UI thread and safely update your application's UI from the UI thread.

<一个href="http://developer.android.com/reference/android/os/AsyncTask.html">http://developer.android.com/reference/android/os/AsyncTask.html

这篇关于简单的SSH连接与JSch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆