我目前正在开发一个应用程序,该应用程序打印远程服务器目录中的文件列表,并能够单击指定的文件并下载它。
到目前为止,这就是我所拥有的:
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;
}
}
但是,当我点击listView中的文件时,我得到一个跟踪错误:
com.jcraft.jsch.JSchException:android.os.NetworkOnMainThreadException
编辑:使用AsyncTask在后台运行它似乎可以修复错误,但如果我这样做,应用程序就无法下载指定文件onClick,除非我硬编码文件名。
任何人都知道如何解决这个问题?
答案 0 :(得分:0)
您正在主应用程序线程上进行网络I / O,如异常所示。如果查看堆栈跟踪,它将指出您在主应用程序线程上执行网络I / O的确切行。
最有可能的是,它来自您的Downloader()
方法,因为您肯定在该方法中执行网络I / O,并且该方法是从onItemClick()
调用的,该方法本身在主应用程序中调用线。请在后台主题上执行此下载。
答案 1 :(得分:0)
您在Downloader(String fileName)
执行onPostExecute(List<String> soundNames)
。但onPostExecute()
在主线程上执行。只有doInBackground()
在单独的线程上执行。将Downloader(String fileName)
的执行移至doInBackground()
。
编辑:或创建一个单独的AsyncTask,在自己的Downloader(String fileName)
doInBackground()
答案 2 :(得分:0)
您应该将任何网络移动到doInBackground中,以便它在AsyncTask的线程中运行