android - what are the paramteres in asynctask in this case? -
i'm new asynctask , first time use it. trying achieve following: have activity has button , textview. when press button app makes ssh connection in background , connects raspberry pi. runs couple of python scripts , uploads them dropbox.
when it's ready, textview warns me can find file in dropbox, upload complete. if i'm right, shouldn't write in asynctask parameter list or should i?
aside argument list, there more on 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 remote host private static string password = "password"; // password of 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 getting downloaded"); btn_ssh.setenabled(false); } }; asynctask.execute(cmd); } }
the parameters asynctask
following:
asynctask<doinbackgroundparams, onprogressupdateparams, onpostexecuteparams>
in case not not using onprogressupdate
, doinbackground
can leave void
. filling arraylist<string> result
in doinbackground
, want use values in onpostexecute
value arraylist<string>
. if not leave void
. in end looks 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 getting downloaded"); btn_ssh.setenabled(false); }
Comments
Post a Comment