2012-06-11 110 views
1

我以前使用ASyncTask並沒有遇到任何問題。我不明白爲什麼這個代碼不起作用。我必須訪問一個ftp服務器才能下載我設置爲TextView的文本文件。 ASyncTask內的代碼工作正常。我已經檢查過了。出於某種原因,當我調用新線程'new GetStory()。execute'時,我遇到了問題。任何幫助都會很棒。AsyncTask執行調試

package com.amazingstories; 

import it.sauronsoftware.ftp4j.FTPAbortedException; 
import it.sauronsoftware.ftp4j.FTPClient; 
import it.sauronsoftware.ftp4j.FTPDataTransferException; 
import it.sauronsoftware.ftp4j.FTPDataTransferListener; 
import it.sauronsoftware.ftp4j.FTPException; 
import it.sauronsoftware.ftp4j.FTPIllegalReplyException; 

import java.io.File; 
import java.io.IOException; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Environment; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.TextView; 
import android.widget.Toast; 

public class DisplayStory extends Activity { 

    String path; 
    TextView story; 
    FTPClient ftp = new FTPClient(); 
    File file; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     path = getIntent().getExtras().getString("path"); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.displaystory); 
     story = (TextView) findViewById(R.id.tvStory); 
     story.setText(path); 
     file = new File(
       Environment 
         .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), 
       "temp.txt");  
     new GetStory().execute(); 
//  try{ 
//  new GetStory().execute(); 
//  }catch(Exception e){ 
//   Toast.makeText(getApplicationContext(), e.toString(), 
//     Toast.LENGTH_SHORT).show(); 
//   
//  } 
    } 

    public class GetStory extends AsyncTask<Void, Integer, Void> { 

     @Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getApplicationContext(), "Try", 
        Toast.LENGTH_SHORT).show(); 
      try { 
       Toast.makeText(getApplicationContext(), "Try", 
         Toast.LENGTH_SHORT).show(); 

       if (!ftp.isConnected()) { 

        ftp.connect("....."); 
        Toast.makeText(getApplicationContext(), 
          "connected to server", Toast.LENGTH_SHORT).show(); 
        ftp.login("....", "....."); 
       } 
       Toast.makeText(getApplicationContext(), "FTP Connected", 
         Toast.LENGTH_SHORT).show(); 
       ftp.changeDirectoryUp(); 
       ftp.download(path, file, new FTPDataTransferListener() { 

        //ProgressDialog dialog; 

        @Override 
        public void aborted() { 
         // TODO Auto-generated method stub 
        // dialog.dismiss(); 
         story.setText("Transfer Aborted"); 
        } 

        @Override 
        public void completed() { 
         // TODO Auto-generated method stub 
         //dialog.dismiss(); 
        } 

        @Override 
        public void failed() { 
         // TODO Auto-generated method stub 
         //dialog.dismiss(); 
         story.setText("Transfer Failed"); 
        } 

        @Override 
        public void started() { 
         // TODO Auto-generated method stub 
         //dialog = ProgressDialog.show(DisplayStory.this, "", 
          // "Loading. Please wait...", true); 
        } 

        @Override 
        public void transferred(int arg0) { 
         // TODO Auto-generated method stub 

        } 

       }); 
       // tv.setText(test.toString()); 
       Toast.makeText(getApplicationContext(), "Done", 
         Toast.LENGTH_SHORT).show(); 
      } catch (FTPException e) { 
       Toast.makeText(getApplicationContext(), 
         "Exception Caught " + e, Toast.LENGTH_LONG).show(); 
      } catch (IllegalStateException e) { 
       // TODO Auto-generated catch block 
       Toast.makeText(getApplicationContext(), 
         "Exception Caught " + e, Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       Toast.makeText(getApplicationContext(), 
         "Exception Caught " + e, Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
      } catch (FTPIllegalReplyException e) { 
       // TODO Auto-generated catch block 
       Toast.makeText(getApplicationContext(), 
         "Exception Caught " + e, Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
      } catch (FTPDataTransferException e) { 
       // TODO Auto-generated catch block 
       Toast.makeText(getApplicationContext(), 
         "Exception Caught " + e, Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
      } catch (FTPAbortedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } finally { 
       try { 
        ftp.logout(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (FTPIllegalReplyException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (FTPException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
      getFileData get = new getFileData(); 
      try { 
       story.setText(get.getData(file)); 
       Toast.makeText(getApplicationContext(), "Set TextView", 
         Toast.LENGTH_SHORT).show(); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      file.delete(); 

     } 

    } 
} 
+2

您正在後臺線程(Toast.makeText())內操作UI。你不能這樣做。使用onProgressUpdate()報告更新消息。 – thaussma

+0

解釋你遇到的問題 –

+0

我的程序在通話方法中崩潰。它不會輸入AsyncTask。 –

回答

0

方法onPreExecute()onPostExecute()onProgressUpdate()是一個AsyncTask執行被稱爲主(UI)線程中的唯一方法,因此是唯一的地方,你可以直接作出UI方法的調用(如TextView.setText()Toast.makeText())。

您的程序很可能會崩潰,因爲您無法在線程中創建一個「Looper尚未準備好」的Toast,這是從doInBackground()發生時發生的事情;這會導致異常和崩潰。

+0

它的工作,謝謝:) –

1
@Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getApplicationContext(), "Try", 
        Toast.LENGTH_SHORT).show(); 

您不允許在doInBackground中調用Toast。 doInBackground方法在非UI線程中運行,因此您無法在UI上操作或sidplay Toast。它必須在onPreExecute或onPostExceute中完成。

+0

它的工作,謝謝:) –