我制作了一款小型Android应用,我的第一款应用。其功能之一是将文本报告上传到ftp服务器。我正在使用apache-commons-net。在某些设备上,ftp服务器关闭连接而没有指示。服务器日志为空。 下面是执行文件传输的内部类的代码。有四个文件要发送,因此循环。 textViewResult控件用于通知用户传输状态。 1.您是否看到可能导致封闭连接的代码存在明显问题? 2."连接关闭,没有指示"错误并没有真正告诉我任何关于问题真正来源的信息。有没有办法得到更详细的错误?
编辑:已在LG G5,H831,android 7.0上报告问题。在LG H635上,Android 6.0可以毫无问题地工作。
edit2:Stacktrace
Data uploading error:
Connection closed without indication.
Connection info:
MOBILE
UMTS
CONNECTED
Stack Trace:
org.apache.commons.net.ftp.FTP.getReply(FTP.java:324)
org.apache.commons.net.ftp.FTP.getReply(FTP.java:300)
org.apache.commons.net.ftp.FTP.connectAction(FTP.java:438)
org.apache.commons.net.ftp.FTPClient.connectAction(FTPClient.java:962)
org.apache.commons.net.ftp.FTPClient.connectAction(FTPClient.java:950)
org.apache.commons.net.SocketClient._connect(SocketClient.java:244)
org.apache.commons.net.SocketClient.connect(SocketClient.java:202)
com.jimmorr.t9a.t9amagicflux.ReportBattleResults$ConnectFTP.doInBackground(ReportBattleResults.java:555)
com.jimmorr.t9a.t9amagicflux.ReportBattleResults$ConnectFTP.doInBackground(ReportBattleResults.java:510)
android.os.AsyncTask$2.call(AsyncTask.java:304)
java.util.concurrent.FutureTask.run(FutureTask.java:237)
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
java.lang.Thread.run(Thread.java:761)
private class ConnectFTP extends AsyncTask<String, Void, String> {
private String resp;
ProgressDialog progressDialog;
protected Long doInBackground(String message) {
return null;
}
@Override
protected String doInBackground(String... strings) {
if(android.os.Debug.isDebuggerConnected())
android.os.Debug.waitForDebugger();
String server = "ftp....";
int port = 21;
String user = "user...";
String pass = "pass...";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
boolean success = ftpClient.changeWorkingDirectory("public_ftp/incoming");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
for (int j = 0; j<2;j++) {
for (int k = 0; k < 8; k = k + 2) {
OutputStream outputStream1;
Log.v(strings[k + 1]+" pass:"+j+" folder change:"+success, strings[k]);
outputStream1 = ftpClient.storeFileStream(strings[k + 1]);
outputStream1.write(strings[k].getBytes());
outputStream1.close();
if (j==0) resp = "main files sending :"+ k+"/4";
else resp = "backup files sending :"+ k+"/4";
if (!ftpClient.completePendingCommand()) {
ftpClient.logout();
ftpClient.disconnect();
System.err.println("File transfer failed.");
throw new IOException();
//System.exit(1);
}
}
success = ftpClient.changeWorkingDirectory("/public_ftp/back_up");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
}
return resp;
} catch (Throwable ex) {
progressDialog.dismiss();
String str = "Data uploading error:\n" + ex.getMessage();
textViewResult.setText(str);
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
return null;
} catch (java.io.IOException ex) {
ex.printStackTrace();
Log.e("FTP connection",ex.getMessage());
}
return null;
}
//return null;
}
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
progressDialog.dismiss();
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(ReportBattleResults.this,
"Sending Battle Report",
"sending data");
}
}