0

I'm new to AsyncTask and this is the first time I use it. What i am trying to achieve is the following: I would like to have an activity which has a button and a textview. When I press the button the app makes an ssh connection in the background and it connects to my raspberry pi. It runs a couple of python scripts and uploads them to dropbox.

When it's ready, the textview warns me that I can find my file in dropbox, as the upload was complete. If I'm right, I shouldn't write anything in the AsyncTask parameter list or should I?

Aside from the argument list, are there more to do on my code?

Thanks in advance!

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);

}
}
johnny cigar
  • 119
  • 10

1 Answers1

1

The parameters for AsyncTask are the following:

  AsyncTask<doInBackgroundParams, onProgressUpdateParams, onPostExecuteParams>

In your case you are not not using onProgressUpdate and doInBackground so you can leave it as Void. You are filling an ArrayList<String> result in doInBackground and probably want to use the values in onPostExecute so the value would be ArrayList<String>. If not leave it as Void. In the end it looks like this

 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

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • Thanks for the quick reply. was it intentional, that you didn't insert the asyncTask.execute(cmd); line? Because I get errors because of this. – johnny cigar Aug 18 '17 at 15:02
  • @johnnycigar It's only the code for the async parameters. You can add that of course. – Murat Karagöz Aug 18 '17 at 15:05
  • Okay, done, the last error: missing return statement from doinBackground (line 87 <-- https://pastebin.com/s21bznRT) I wrote return null but the code doesn't do anything. Could you help? Thanks! – johnny cigar Aug 18 '17 at 16:40
  • As I said you are not doing anything with `List result = new ArrayList();` in your code. You might want to return that. See my first point about that in my answer. – Murat Karagöz Aug 18 '17 at 17:44