覆盖AsyncTask中的onPostExecute()

时间:2015-04-06 10:58:00

标签: android android-asynctask

我正在尝试学习如何使用AsyncTask。我在doInBackground中有代码返回一个String数组,一个回调调用一个接口将它返回给MainActivity。我正在成功执行AsyncTask。

问题是:尝试通过onPostExecute返回数组我得到了一个参数错误;它不会接受数组。如何使用onPostExecute方法返回值feeds?我试图更改doInBackground的结果,但也不接受数组。

以下是MainActivity中调用的界面:

interface DownloadFinishedListener {
void notifyDataRefreshed(String[] feeds);}

这是回调:

try {
        mCallback = (DownloadFinishedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement DownloadFinishedListener");
    }

这里是AsyncTask:

public class DownloaderTask extends AsyncTask<Integer, Void, String> {


    @Override
    protected String doInBackground(Integer... params) {
        downloadTweets(params);
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        mCallback.notifyDataRefreshed(s); //trying to send back feeds
    }

    // Simulates downloading Twitter data from the network


    private String[] downloadTweets(Integer resourceIDS[]) {
        final int simulatedDelay = 2000;
        String[] feeds = new String[resourceIDS.length];
        try {
            for (int idx = 0; idx < resourceIDS.length; idx++) {
                InputStream inputStream;
                BufferedReader in;
                try {
                    // Pretend downloading takes a long time
                    Thread.sleep(simulatedDelay);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                inputStream = mContext.getResources().openRawResource(
                        resourceIDS[idx]);
                in = new BufferedReader(new InputStreamReader(inputStream));

                String readLine;
                StringBuffer buf = new StringBuffer();

                while ((readLine = in.readLine()) != null) {
                    buf.append(readLine);
                }

                feeds[idx] = buf.toString();

                if (null != in) {
                    in.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return feeds;
    }


}

2 个答案:

答案 0 :(得分:2)

  

问题是:尝试通过onPostExecute返回数组我是   得到一个参数错误;它不会接受数组

您需要将String Array作为最后一个通用参数传递给AsyncTask,它返回类型为doInBackground,参数类型为onPostExecute

进行以下更改:

1。AsyncTask扩展为:

public class DownloaderTask extends AsyncTask<Integer, Void, String[]>

2。doInBackground方法返回类型从String更改为String[]

 @Override
    protected String[] doInBackground(Integer... params) {
        downloadTweets(params);
        return downloadTweets(params);
    }

3。onPostExecute参数类型从String更改为String[]

protected void onPostExecute(String[] s) {
        super.onPostExecute(s);
        mCallback.notifyDataRefreshed(s); //trying to send back feeds
   }

答案 1 :(得分:1)

您应该在String[]中将AsyncTask作为最后一个参数传递,而不仅仅是String,而AsyncTask应该与此类似:

public class DownloaderTask extends AsyncTask<Integer, Void, String[]> {

    @Override
    protected String[] doInBackground(Integer... params) {
        return downloadTweets(params);;
    }

    @Override
    protected void onPostExecute(String[] s) {
        super.onPostExecute(s);
        mCallback.notifyDataRefreshed(s); //trying to send back feeds
    }
}