Android Apache FTP客户端在创建新的FTPClient对象后停止

Android Apache FTP客户端在创建新的FTPClient对象后停止,android,apache,ftp,Android,Apache,Ftp,我正在尝试完成我的第一个android应用程序。 到目前为止,下面的代码在标准java环境中运行良好。(它会在ftp服务器上检查最新文件,然后下载。) apachecommons.jar被复制到我的项目的libs文件夹中。 该应用程序可以访问互联网并在SD卡上读/写 在任何android设备上,无论出于何种原因创建新的FTP客户端后,它都会停止 public void ftpDownload() { try { //new ftp client

我正在尝试完成我的第一个android应用程序。
到目前为止,下面的代码在标准java环境中运行良好。(它会在ftp服务器上检查最新文件,然后下载。)

apachecommons.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 We中显示的异常:

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.

根据您发布的日志,
StrictMode$AndroidBlockGuardPolicy.onNetwork

您需要将创建FTP会话的所有代码放入。因为,100%确定,您尝试在GUI线程上运行ftp客户端


希望它能帮助你

你说的“停止”是什么意思?崩溃,卡住,请添加日志。感谢您添加了在虚拟设备上运行Logcat时在Logcat中得到的异常。是否打印了所有日志?方法ftpDownlaod()通过按下按钮启动。是的,这就是我在日志中看到的,只是试了一下。作品(Y)