簡體   English   中英

下載文件時,Android應用會不定期地凍結

[英]Android app freezes irregularly when downloading files

我正在制作一個Android應用程序,用戶可以在其中從FTP服務器下載文件。 對於ftp部分,我正在使用apache.org.commons-net軟件包。

連接到服務器后,我會得到文件名列表,然后要下載每個文件。 我啟動下載例程,該例程在其自己的線程中運行。

我遇到的問題是,如果服務器上有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();
        }
    }
};

OK,發現問題:

當boolean downloadIsNewer為false時,我不會調用client.completePendingCommand()。 當我在is.close()行之前添加它時,它就像一個符咒。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM