在这种情况下,asynctask中的参数是什么?

时间:2017-08-18 14:34:30

标签: android ssh android-asynctask

我是AsyncTask的新手,这是我第一次使用它。我想要实现的目标如下: 我想要一个有按钮和textview的活动。当我按下按钮时,应用程序在后台进行ssh连接,并连接到我的覆盆子pi。它运行几个python脚本并将它们上传到dropbox。

当它准备就绪时,textview警告我,我可以在dropbox中找到我的文件,因为上传已完成。如果我是对的,我不应该在AsyncTask参数列表中写任何东西,还是我应该?

除了参数列表之外,还有更多关于我的代码吗?

提前致谢!

public class Aszin extends AppCompatActivity {

String cmd = "python /home/pi/Desktop/get_db_pic.py";
private static String USERNAME = "piro"; // username for remote host
private static String PASSWORD = "password"; // password of the remote host
private static String host  = "192.168.xx.xx"; // remote host address
private static int port = 22;

TextView tv1;
Button btn_ssh;
private AsyncTask<String, Integer, ???> asyncTask;

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

    tv1 = (TextView) findViewById(R.id.tv1);
    tv1.setText("default");

    btn_ssh = (Button) findViewById(R.id.btn1);
    btn_ssh .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            connect2pi(cmd);
        }
    });

}

public void connect2pi(final String cmd){
    asyncTask = new AsyncTask<String, Integer, ??>() {

        @Override
        protected void doInBackground() {
            List<String> result = new ArrayList<String>();
            try {
                JSch jsch = new JSch();
                Session session = jsch.getSession(USERNAME, host, port);

                session.setConfig("StrictHostKeyChecking", "no");
                session.setPassword(PASSWORD);

                session.connect();

                ChannelExec channelExec = (ChannelExec) session.openChannel("exec");

                InputStream in = channelExec.getInputStream();

                channelExec.setCommand(cmd);
                channelExec.connect();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));

                String line;
                while ((line = reader.readLine()) != null) {
                    result.add(line);

                }
                channelExec.getExitStatus();

                channelExec.disconnect();

                session.disconnect();

            } catch (Exception e) {
                System.err.println("Error: " + e);
            }
        }


        @Override
        protected void onPostExecute(JSONObject result) {
            super.onPostExecute(result);
            tv1.setText("your file has been downloaded");
            btn_ssh.setEnabled(true);

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            tv1.setText("your file is getting downloaded");
            btn_ssh.setEnabled(false);

        }

    };
    asyncTask.execute(cmd);

}
}

1 个答案:

答案 0 :(得分:1)

AsyncTask的参数如下:

  AsyncTask<doInBackgroundParams, onProgressUpdateParams, onPostExecuteParams>

在您的情况下,您没有使用onProgressUpdatedoInBackground,因此您可以将其保留为Void。您在ArrayList<String> result填写了doInBackground,可能希望使用onPostExecute中的值,因此值为ArrayList<String>。如果不将其保留为Void。 最后它看起来像这个

 asyncTask = new AsyncTask<Void, Void, Void>() {

    @Override
    protected void doInBackground(Void... params) {
        List<String> result = new ArrayList<String>();
        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(USERNAME, host, port);

            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(PASSWORD);

            session.connect();

            ChannelExec channelExec = (ChannelExec) session.openChannel("exec");

            InputStream in = channelExec.getInputStream();

            channelExec.setCommand(cmd);
            channelExec.connect();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));

            String line;
            while ((line = reader.readLine()) != null) {
                result.add(line);

            }
            channelExec.getExitStatus();

            channelExec.disconnect();

            session.disconnect();

        } catch (Exception e) {
            System.err.println("Error: " + e);
        }
    }


    @Override
    protected void onPostExecute(Void param) {
        tv1.setText("your file has been downloaded");
        btn_ssh.setEnabled(true);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        tv1.setText("your file is getting downloaded");
        btn_ssh.setEnabled(false);
    }

See the docs here