0

i'm trying to get my fist android app done.
So far the code below works perfecty in a standard java environment. (It checks for the newest file on a ftp server and downloads it afterwards.)

The apache commons .jar is copied to the libs folder of my project.
The app is allowed to access the internet and read/write on the sdcard.

On any android device it stops after creating an new FTP Client for whatever reason.

    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();
    }
}

Thats the Exception displayed in Logcat wehen i run in on my virtual device:

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.
ottse
  • 749
  • 6
  • 8

1 Answers1

2

According to logs you posted, StrictMode$AndroidBlockGuardPolicy.onNetwork

You need put all code that creates FTP session in AsyncTask. Because, sure 100%, you try to run ftp client on GUI thread.

Hope it will help you

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225