Android 在AsyncTask中重写onPostExecute()
我正在努力学习如何使用AsyncTask。我在Android 在AsyncTask中重写onPostExecute(),android,android-asynctask,Android,Android Asynctask,我正在努力学习如何使用AsyncTask。我在doInBackground中有返回字符串数组的代码,以及调用接口将其返回到MainActivity的回调。我正在成功执行异步任务 问题是:试图通过onPostExecute返回数组时,我得到了一个参数错误;它不会接受数组。如何使用onPostExecute方法返回值feeds?我试图更改doInBackground的结果,但也不接受数组 以下是在MainActivity中调用的接口: interface DownloadFinishedListen
doInBackground
中有返回字符串数组的代码,以及调用接口将其返回到MainActivity的回调。我正在成功执行异步任务
问题是:试图通过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");
}
下面是异步任务:
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;
}
}
公共类下载任务扩展异步任务{
@凌驾
受保护字符串doInBackground(整数…参数){
下载tweets(params);
返回null;
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
mCallback.notifyDataRefreshed;//正在尝试发回提要
}
//模拟从网络下载Twitter数据
私有字符串[]下载tweets(整数资源ID[]){
最终模拟时间=2000年;
String[]提要=新字符串[ResourceId.length];
试一试{
for(int-idx=0;idx
问题是:试图通过onPostExecute返回数组
获取参数错误;它不会接受数组
您需要将字符串数组作为最后一个通用参数传递给AsyncTask,它的返回类型为doInBackground
,参数类型为onPostExecute
执行以下更改:
1.将异步任务扩展为:
public class DownloaderTask extends AsyncTask<Integer, Void, String[]>
3.将onPostExecute
参数类型从String
更改为String[]
:
@Override
protected String[] doInBackground(Integer... params) {
downloadTweets(params);
return downloadTweets(params);
}
protected void onPostExecute(String[] s) {
super.onPostExecute(s);
mCallback.notifyDataRefreshed(s); //trying to send back feeds
}
您应该将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
}
}
公共类下载任务扩展异步任务{
@凌驾
受保护字符串[]doInBackground(整数…参数){
返回下载tweets(params);;
}
@凌驾
受保护的void onPostExecute(字符串[]s){
super.onPostExecute(s);
mCallback.notifyDataRefreshed;//正在尝试发回提要
}
}
还需要将doInBackground
方法的返回类型从String
更改为String[]
谢谢。我试过使用String…
,这给了我各种各样的错误。”字符串[]似乎已经处理好了它,以及其他建议的编辑。我可以得到关于第2点的澄清吗?如果调用下载tweets(params);返回下载tweets(params)
将调用该方法两次,对吗?@Dithanial:是的,从downloadTweets methodOk返回的doInBackground
返回字符串[]数组,但我不需要调用downloadTweets()
方法两次。我可以用返回下载tweets(params)只做一次代码>对吗?@Dithanial:对,对。同时添加null
检查params
值