Osama Sallam Osama Sallam - 16 days ago 5
Android Question

android make an object from extends AsyncTask and send it to another activity

I have problem with create an object from sshconnection that extends AsyncTask class..

My class is as below

public class sshconnection extends AsyncTask<String, Void, String> {
ByteArrayOutputStream baos;
protected String doInBackground(String... params) {

try {
String results;

JSch jsch = new JSch();
Session session = jsch.getSession(User, IP, Port);
session.setPassword(Pass);

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

session.connect();


ChannelExec channel = (ChannelExec)session.openChannel("exec");
InputStream in = channel.getInputStream();
baos = new ByteArrayOutputStream();
channel.setOutputStream(baos);
channel.setCommand(CMD);
channel.connect();



byte[] tmp=new byte[1024];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
System.out.print(new String(tmp, 0, i));
}
if(channel.isClosed()){
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}

channel.disconnect();
session.disconnect();


return baos.toString();


} catch (Exception e) {
//Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
//System.out.println(e.getMessage());
}
return null;


}

@Override
protected void onPostExecute(String result) {
// TextView txt = (TextView) findViewById(R.id.txtipinfo);
// txt.setText("Executed");
Toast.makeText(MainActivity.this, baos.toString(), Toast.LENGTH_LONG).show();

}


}


and this is when i called it in button click

btn = (Button) findViewById(R.id.button_conn);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

CMD = "ls";

new sshconnection().execute(User,Pass,IP,CMD);

}
});


Until here my code is work fine and I can make ssh connection to my server.

My problem is that I want to create an object from sshconnection class to send it to another activity to reuse the connection of the ssh to sends other ssh commands to my server.

If there is another way to do that even if i have to change AsyncTask class .. Or use anthing else like new threads.
Please tell me how to do that and where i make changes in my code.

Thanks a lot for everyone.

Answer

I found the solution

public class sshconnection extends AsyncTask<String, Void, String> {
        public String returncmd=null;



        ByteArrayOutputStream baos;
        protected String doInBackground(String... params) {

            try {

                //Sndlist=args[0];
                //result=-11;
                //String results;

                JSch jsch = new JSch();
                Session session = jsch.getSession("admin", "10.0.0.1",22);

                session.setPassword("a");

                //Toast.makeText(null," user = " + ss, Toast.LENGTH_SHORT).show();

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

                session.connect();


            ChannelExec channel = (ChannelExec)session.openChannel("exec");
            InputStream in = channel.getInputStream();
            baos = new ByteArrayOutputStream();
            channel.setOutputStream(baos);
            channel.setCommand("/ip hotspot user add name=nnnnnn profile=120");
            channel.connect();



            byte[] tmp=new byte[1024];
            while(true){
                while(in.available()>0){
                    int i=in.read(tmp, 0, 1024);
                    if(i<0)break;
                    System.out.print(new String(tmp, 0, i));
                }
                if(channel.isClosed()){
                    System.out.println("exit-status: "+channel.getExitStatus());
                    break;
                }
                try{Thread.sleep(1000);}catch(Exception ee){}
            }

            channel.disconnect();
            session.disconnect();

            returncmd = baos.toString();
            return returncmd;


            } catch (Exception e) {
                //Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                //System.out.println(e.getMessage());
            }
            return null;


        }

        @Override
        protected void onPostExecute(String result) {
            //  TextView txt = (TextView) findViewById(R.id.txtipinfo);
            //  txt.setText("Executed");
            //mProgressDialog.dismiss();
            //Toast.makeText(UserDetail.this, baos.toString(), Toast.LENGTH_LONG).show();

        }



    } // end of class sshconnection

and you can call as follow:

new sshconnection().execute;

Comments