2012-09-05 33 views
-1

我將一些代碼從普通方法移到了AsyncTask doInBackground方法中,除filereader之外,一切正常。我得到「上下文無法解決」。 我試着把「context.getApplicationContext()」放在一起,但這只是讓我「類型AlarmReceiver.backgroundputFTP的方法openFileInput(字符串)未定義」。任何人的想法請好嗎?AsyncTask中的FileInputStream錯誤

public void putFTP(Context context) 
    { 
     new backgroundputFTP().execute(); 
    } 

    private class backgroundputFTP extends AsyncTask< Void, Void,Void > 
    { 

     @Override 
     protected Void doInBackground(Void... params) 
     { 
    // 
// Push query result text file to FTP server 
// 

    FTPClient client = new FTPClient(); 
    FileInputStream fis = null; 
    Looper.prepare(); 

    try { 
     client.connect(ipAddress); 
     client.enterLocalPassiveMode(); 
     client.login(user, pass); 

     // 
     // Create an InputStream of the file to be uploaded 
     // 
     filename = "sdcardstats.txt"; 
     fis = context.getApplicationContext().openFileInput(filename); 

回答

0

試試這個:

fis = YourActivity.this.getContext().openFileInput(filename); 

YourActivity是在其中你有你的AsyncTask

+0

感謝試圖幫助,但結果「的方法的getContext()是未定義的類型AlarmReceiver」 – Kevmeister

+0

@Kevmeister然後,定義在AlarmReceiver現場上下文mContext,做mContext =上下文您putFTP ,首先,然後使用AsyncTask中的mContext.openFileInput –

0
fis = context.getApplicationContext().openFileInput(filename); 

你上哪兒去定義這方面Activity?嘗試這個 。

public Context cContext ; 
public void putFTP(Context context) 
    { 
      cContext = context; 
     new backgroundputFTP().execute(); 
    } 

    private class backgroundputFTP extends AsyncTask< Void, Void,Void > 
    { 

     @Override 
     protected Void doInBackground(Void... params) 
     { 
    // 
// Push query result text file to FTP server 
// 

    FTPClient client = new FTPClient(); 
    FileInputStream fis = null; 
    Looper.prepare(); 

    try { 
     client.connect(ipAddress); 
     client.enterLocalPassiveMode(); 
     client.login(user, pass); 

     // 
     // Create an InputStream of the file to be uploaded 
     // 
     filename = "sdcardstats.txt"; 
     fis = cContext.openFileInput(filename);