我正试图让我的第一个Android应用程序完成 到目前为止,下面的代码在标准java环境中运行良好。 (它检查ftp服务器上的最新文件,然后下载。)
将apache commons .jar复制到我项目的libs文件夹中 该应用程序可以访问互联网并在SD卡上读/写。
在任何Android设备上,无论出于何种原因,它都会在创建新的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.
答案 0 :(得分:1)
根据您发布的日志StrictMode$AndroidBlockGuardPolicy.onNetwork
您需要将所有创建FTP会话的代码放在AsyncTask中。因为,确保100%,您尝试在GUI线程上运行ftp客户端。
希望它能帮到你