1

如何将此代码写入 Thread 或 AsyncTask ?

JSch jsch = new JSch();
    Session session = null;
        session = jsch.getSession("******", "*********",22);
        session.setConfig("StrictHostKeyChecking", "no");     
        session.setPassword("******");
        session.connect();
        Channel channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;     
        sftpChannel.exit();
        session.disconnect();
4

2 回答 2

2

要将其写入线程,只需创建一个包含 Runnable 进程的新线程对象。

Thread connectionThread = new Thread(new Runnable() {
    public void run() {
        JSch jsch = new JSch();
        Session session = null;
        session = jsch.getSession("******", "*********",22);
        session.setConfig("StrictHostKeyChecking", "no");     
        session.setPassword("******");
        session.connect();
        Channel channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;     
        sftpChannel.exit();
        session.disconnect();
    }
});

然后可以随时运行

connectionThread.start();

关于线程、ASyncTasks 和其他进程的更多信息可以在这里找到:http: //developer.android.com/guide/components/processes-and-threads.html

于 2012-10-11T18:58:13.290 回答
0

从理论上讲,您只需将其放在 doInBackground 中即可进行 AsyncTask

于 2012-10-11T19:18:34.970 回答