2015-06-03 86 views
0

在Asynctask中,進度條正在工作或文件正在成功上傳。 當我們在COMMENT1行之前寫入,但進度條不起作用時,COMMENT2行正確地上傳文件。 但是,如果我們在COMMENT1之後編寫COMMENT2,那麼我們將面對ERROR代碼500(語法錯誤)並且上傳失敗,但進度條 正常工作。進度條對話框不支持在android中上傳FTP

private class AsyncCaller extends AsyncTask<String, Integer, String> { 

int bytesRead, bytesAvailable, bufferSize = 1024,progress; 
byte[] buffer; 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    mProgressDialog = new ProgressDialog(getActivity()); 
    mProgressDialog.setMessage("Uploading file.."); 
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
    mProgressDialog.setIndeterminate(false); 
    mProgressDialog.setCancelable(false); 
    mProgressDialog.setProgress(0); 
    mProgressDialog.setMax(100); 
    mProgressDialog.show(); 

} 

@Override 
protected String doInBackground(String... params) { 
    // TODO Auto-generated method stub 
    // connection(); 
    FTPClient con = null; 

     try 
     { 
      con = new FTPClient(); 
      con.connect(FTP_HOST); 

      if (con.login(FTP_USER, FTP_PASS)) 
      { 
       con.enterLocalActiveMode(); // important! 
       con.setFileType(FTP.BINARY_FILE_TYPE); 
       con.changeWorkingDirectory("/uploads/school-staging/files/"); 
       String data = FilePath; 
       final DataOutputStream out = new DataOutputStream(con.getOutputStream()); 
       BufferedInputStream in = new BufferedInputStream(new FileInputStream(data)); 
       int bufferSize=1024; 
       byte[] buffer = new byte[bufferSize]; 
       // Read file 

       bytesRead = in.read(buffer, 0, bufferSize);//COMMENT 1 
       progress=0; 

       System.out.println("BYTE READ="+bytesRead); 

       while (bytesRead > 0) 
       { 

        progress+=bytesRead; 
        System.out.println("PROGRESS="+progress); 
        out.write(buffer, 0, bytesRead); 
        bytesAvailable = in.available(); 
        publishProgress((int)((progress*100)/(file.length()))); 
        bufferSize = Math.min(bytesAvailable, bufferSize); 

        bytesRead = in.read(buffer, 0, bufferSize); 

       } 
      boolean result = con.storeFile(FileName, in);//COMMENT 2 
       int code= con.getReplyCode(); 
         System.out.println("CODE="+code); 
       in.close(); 
       publishProgress(100); 

       if (result) Log.v("upload result", "succeeded"); 
       System.out.println("RESULT="+result); 
       con.logout(); 
       con.disconnect(); 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

    return "ok"; 
} 

@Override 
public void onProgressUpdate(Integer... values) 
    { 
     super.onProgressUpdate(values); 
     mProgressDialog.setProgress(values[0]); 
    }//end of onProgressUpdate 


@Override 
protected void onPostExecute(String result) { 
    super.onPostExecute(result); 
    connection(); 
    mProgressDialog.dismiss(); 
} 

回答

0

我遇到了類似的問題。這樣做的原因似乎是onPreExecute()中的ui代碼無法通過android系統呈現,並且doInBackground()中的代碼佔用了cpu。您可以關閉onPostExecute()中的對話框,但...

您必須在創建AsyncCaller之前顯示進度對話框,將進度對話框傳遞給AsyncCaller,並且可以在onPostExecute()中將其解除。

ProgressDialog pd = new ProgressDialog(getActivity()); 
    pd.setTitle("title string"); 
    pd.setIndeterminate(true); 
    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
    pd.show(); 

    new AsyncCaller(getActivity(), pd).execute(params); 

    in onPostExecute(): 
     if (pd != null && pd.isShowing()) 
      pd.dismiss();