I'm currently working on an app that prints a list of file from a remote server directory and being able to click on a specified file and download it.
So far this is what I have:
public class MainActivity extends Activity {
private String user = "username";
private String pass = "password";
private String host = "hostname";
private int portNum = 22;
private static final String SFTPWORKINGDIR = "/path/to/file";
private String fileName;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView list = (ListView)findViewById(R.id.choose_sound_listView);
new AsyncTask<Void, Void, List<String>>() {
@Override
protected List<String> doInBackground(Void... params) {
try {
return executeRemoteCommand(user, pass, host, portNum);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void onPostExecute(List<String> soundNames){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1, android.R.id.text1, soundNames);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
String newFileName = (String) list.getItemAtPosition(position);
fileName = newFileName;
Downloader(fileName);
// Show Alert
Toast.makeText(getApplicationContext(),
"Downloaded " + fileName , Toast.LENGTH_LONG).show();
}
});
}
}.execute();
}
public List<String> executeRemoteCommand(String username, String password, String hostname, int port) throws Exception {
List<String> soundNames = new ArrayList<String>();
JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, port);
session.setPassword(password);
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
String command = "ls -la | awk '{ print $9}'";
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
System.out.println("Connect to session...");
channel.connect();
StringBuffer buffer = new StringBuffer();
//This is the recommended way to read the output
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
buffer.append(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();
Scanner scanner = new Scanner(buffer.toString());
while (scanner.hasNextLine()){
String line = scanner.nextLine();
soundNames.add(line);
Log.i("SSH", ": " + line);
}
return soundNames;
}
public void Downloader(String fileName) {
JSch jsch = new JSch();
Session session = null;
Channel channel = null;
ChannelSftp sftpChannel = null;
try {
session = jsch.getSession(user, host, portNum);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(pass);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
sftpChannel = (ChannelSftp) channel;
sftpChannel.cd(SFTPWORKINGDIR); //cd to dir that contains file
try {
byte[] buffer = new byte[1024];
BufferedInputStream bis = new BufferedInputStream(sftpChannel.get(fileName));
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS);
File newFile = new File(path, "/" + fileName);
Log.d("Dir" , " " + newFile);
OutputStream os = new FileOutputStream(newFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
int readCount;
while( (readCount = bis.read(buffer)) > 0) {
Log.d("Downloading", " " + fileName );
bos.write(buffer, 0, readCount);
}
bis.close();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d( "SUCCESS ", fileName + " has been downloaded!!!!");
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
But, when I click on the file in the listView, I get a trace error of :
com.jcraft.jsch.JSchException: android.os.NetworkOnMainThreadException
EDIT: Running it on background with AsyncTask seems to fix the error, but if I do that, the app won't be able to download a specified file onClick, unless I hardcode the name of the file.
Anyone know how I can get around this?
You execute your Downloader(String fileName)
in onPostExecute(List<String> soundNames)
. But onPostExecute()
executes on main thread. Only doInBackground()
executes on separate thread. Move the execution of Downloader(String fileName)
to doInBackground()
.
EDIT: or create a separate AsyncTask, which executes Downloader(String fileName)
in own doInBackground()