2013-09-22 40 views
0

我試圖讓我的拳頭安卓應用完成。
到目前爲止,下面的代碼在標準的Java環境中完美運行。 (它會檢查ftp服務器上的最新文件,然後將其下載。)Android Apache FTP客戶端創建新的FTPClient對象後停止

apache commons .jar被複制到我的項目的libs文件夾中。
該應用程序被允許訪問互聯網和在SD卡上讀/寫。

在任何安卓設備上,無論出於何種原因創建新的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的wehen我我的虛擬設備上運行,在彈出的例外:

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

你是什麼意思「停」?崩潰,卡住了,請添加日誌。謝謝 –

+0

當我在虛擬設備上運行它時,只是添加了我在Logcat中獲得的異常。 – ottse

+0

是否打印過所有日誌? –

回答

1

根據您發佈日誌,StrictMode$AndroidBlockGuardPolicy.onNetwork

你需要把在創建FTP會話的所有代碼AsyncTask。因爲肯定是100%,所以你嘗試在GUI線程上運行ftp客戶端。

希望它能幫到你

+0

只是試了一下。作品! (Y) – ottse