【发布时间】:2012-03-13 19:14:35
【问题描述】:
所以基本上我试图使用 AsyncTask 解析来自网站的一些数据,我希望它从程序中获取网站 url,用下载的数据更新 ui 并显示 progressdialog(旋转轮)所以我想我需要这样做:(更新):k 现在可以使用变量,但程序强制关闭
private class backgroundDATA extends AsyncTask<String, Void, Void> {
ProgressDialog dialog;
Document doc;
@Override
protected void onPreExecute() {
dialog = dialog.show(Result.this, " ",
" Loading. Please wait ... ", true);
}
@Override
protected Void doInBackground(String... params) {
try {
doc = Jsoup.connect(params[0]).get();
} catch (IOException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
Elements maine;
Elements titleJSOUP;
Elements recipeJSOUP;
Elements instructionsJSOUP;
String recipE;
maine = doc.select("div#recipeContent");
titleJSOUP = doc.select("title");
recipeJSOUP = maine.select("ul.recipe");
instructionsJSOUP = maine.select("p.instructions");
recipE = recipeJSOUP.toString();
drinkNameText.setText("THE "
+ Jsoup.parse(titleJSOUP.toString()).text()
);
dontListenText.setText(Jsoup.parse(titleJSOUP.toString()).text()
);
recipeText.setText(prepareDRINK(recipE));
instructionsText.setText(Jsoup.parse(instructionsJSOUP.toString())
.text());
dialog.dismiss();
}
}
【问题讨论】:
-
在 onPostExecute 期间不能访问
backgroundDATA的成员吗? main、doc、imgURL 等? -
它告诉我它不能被解析为变量,在这个异步任务之外声明它会使程序强制关闭
-
确定是
main等成员无法解析吗?除非您的AsyncTask被定义为Activity中的内部类,否则它似乎更有可能是诸如drinkNameText之类的UI 元素。 -
它是在 Result.java 中定义并在 onCreate 方法中调用的内部类,我认为上面的代码应该可以工作,但是调用 new backgroundDATA.execute(webiste) 会导致强制关闭
标签: android asynchronous android-asynctask jsoup