我正在尝试完成我的拳头 android 应用程序。
到目前为止,下面的代码在标准的 java 环境中运行良好。(它检查 ftp 服务器上的最新文件,然后下载。)
apache commons .jar 被复制到我项目的 libs 文件夹中。
该应用程序被允许访问互联网并在 sdcard 上读/写。
在任何安卓设备上,无论出于何种原因,它都会在创建新的 FTP 客户端后停止。
public void ftpDownload() {
try {
//new ftp client
FTPClient ftp = new FTPClient();
//try to connect
String url = "string url";
String user = "string username";
String pass = "string password";
ftp.connect(url);
//login to server
if (!ftp.login(user, pass)) {
ftp.logout();
}
int reply = ftp.getReplyCode();
//FTPReply stores a set of constants for FTP reply codes.
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
ftp.enterLocalPassiveMode();
ftp.changeWorkingDirectory("subfolder");
FTPFile[] ftpFiles = ftp.listFiles();
//get newest .xml file name from ftp server
Date lastMod = ftpFiles[0].getTimestamp().getTime();
FTPFile choice = ftpFiles[0];
for (FTPFile file : ftpFiles) {
if (file.getTimestamp().getTime().after(lastMod)) {
choice = file;
lastMod = file.getTimestamp().getTime();
}
}
//get output stream
OutputStream output;
output = new FileOutputStream("/sdcard/Folder/Folder" + "/" + choice.getName());
//get the file from the remote system
ftp.retrieveFile(choice.getName(), output);
String filepath = "/sdcard/Folder/Folder" + "/" + choice.getName();
//close output stream
output.close();
ftp.logout();
ftp.disconnect();
//calls method to parse the downlaoded file
File fXmlFile = new File(filepath);
readXmlFile(fXmlFile);
} catch (Exception ex) {
ex.printStackTrace();
}
}
当我在我的虚拟设备上运行时,这就是 Logcat 中显示的异常:
09-19 15:59:07.067 2057-2057/name of the package W/System.err﹕ android.os.NetworkOnMainThreadException
09-19 15:59:07.077 2057-2057/name of the package W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
09-19 15:59:07.077 2057-2057/name of the package W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-19 15:59:07.087 2057-2057/name of the package W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-19 15:59:07.097 2057-2057/name of the package W/System.err﹕ at java.net.InetAddress.getByName(InetAddress.java:289)
09-19 15:59:07.097 2057-2057/name of the package W/System.err﹕ at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
09-19 15:59:07.108 2057-2057/name of the package W/System.err﹕ at org.apache.commons.net.SocketClient.connect(SocketClient.java:296)
09-19 15:59:07.117 2057-2057/name of the package W/System.err﹕ at name of the package.MainActivity.ftpDownload(MainActivity.java:237)
09-19 15:59:07.117 2057-2057/name of the package W/System.err﹕ at name of the package.MainActivity$1.onClick(MainActivity.java:54)
09-19 15:59:07.127 2057-2057/name of the package W/System.err﹕ at android.view.View.performClick(View.java:4204)
09-19 15:59:07.127 2057-2057/name of the package W/System.err﹕ at android.view.View$PerformClick.run(View.java:17355)
09-19 15:59:07.127 2057-2057/name of the package W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:725)
09-19 15:59:07.137 2057-2057/name of the package W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
09-19 15:59:07.147 2057-2057/name of the package W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
09-19 15:59:07.147 2057-2057/name of the package W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5041)
09-19 15:59:07.159 2057-2057/name of the package W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
09-19 15:59:07.159 2057-2057/name of the package W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
09-19 15:59:07.167 2057-2057/name of the package W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-19 15:59:07.177 2057-2057/name of the package W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-19 15:59:07.177 2057-2057/name of the package W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
09-19 15:59:07.187 2057-2057/name of the package I/Choreographer﹕ Skipped 31 frames! The application may be doing too much work on its main thread.