2012-09-05 38 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);