2011-05-18 55 views
0

我正在制作一个Android应用程序,用户可以从FTP服务器下载文件。对于ftp部分,我使用apache.org.commons-net软件包。下载文件时Android应用程序不规则地冻结

当我连接到服务器时,我得到一个文件名列表,然后我想下载每个文件。我开始在它自己的一个线程中运行的下载例程。

我遇到的问题是,如果我在服务器上说6个文件,并且在我的模拟器上运行此代码,它会下载前两个文件,然后冻结(进度条挂在34%)。当我在手机上运行它时,它会下载三个文件并冻结。

如果我通过模拟器上的代码调试我的方式,它会下载所有六个文件就好,不会冻结。

有没有人有任何想法可能是什么问题?

由于提前, LordJesus

这是我的代码(客户端已经初始化):

private void downloadFiles2() { 
    dialog = new ProgressDialog(this); 
    dialog.setCancelable(true); 
    dialog.setMessage("Loading..."); 
    // set the progress to be horizontal 
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
    // reset the bar to the default value of 0 
    dialog.setProgress(0); 
    dialog.setMax(DownloadCount); 
    // display the progressbar 
    dialog.show(); 

    // create a thread for updating the progress bar 
    Thread background = new Thread (new Runnable() { 
     public void run() { 
      InputStream is = null; 
      try { 
       for (String filename : fileNames) { 
        is = client.retrieveFileStream(filename); 
        byte[] data = new byte[1024]; 
        int x = 0; 
        x = is.read(data, 0, 1024); 
        boolean downloadIsNewer = true; 
        File fullPath = new File(path + "/" + filename); 
        Log.d("FTP", "Starting on " + filename); 
        if (fullPath.exists()) { 
         downloadIsNewer = checkIfNewer(data, fullPath); 
        } 

        if (downloadIsNewer) { 
         Log.d("FTP", "Need to download new file"); 
         FileOutputStream fos = new FileOutputStream(fullPath); 
         fos.write(data,0,x); 
         while((x=is.read(data,0,1024))>=0){ 
          fos.write(data,0,x); 
         } 
         fileText += filename + " - downloaded OK." + FileParser.newline; 
         is.close(); 
         client.completePendingCommand(); 
         fos.flush(); 
         fos.close();       
        } 
        else { 
         Log.d("FTP", "No need to download"); 
         is.close(); 
         fileText += filename + " - own copy is newer." + FileParser.newline; 
        } 
        // active the update handler 
        progressHandler.sendMessage(progressHandler.obtainMessage()); 
       } 
       client.logout(); 
       client.disconnect(); 
      } 

      catch (Exception e) { 
       // if something fails do something smart 
      } 
     } 
    }); 
    // start the background thread 
    background.start(); 
} 

Handler progressHandler = new Handler() { 
    public void handleMessage(Message msg) { 
     dialog.incrementProgressBy(1); 
     InfoTextView.setText(fileText); 
     if(dialog.getProgress()== dialog.getMax()) 
     { 

      dialog.dismiss(); 
     } 
    } 
}; 

回答

0

OK,发现了问题:

当布尔downloadIsNewer是假的我不要调用client.completePendingCommand()。当我在这行之前添加那个.close()它就像一个魅力。