기본적으로 AsyncTask를 사용하여 웹 사이트의 일부 데이터를 구문 분석하려고하면 프로그램에서 웹 사이트 URL을 가져오고 다운로드 된 데이터로 UI를 업데이트하고 progressdialog (회전 휠)를 표시하여 작업을 수행해야한다고 생각했습니다. 그렇게 : (UPDATE) : ... 그럼 이것은 시작에 대한 NullPointerException
을 일으킬 것입니다 어쨌든AsyncTask 및 JSOUP 구문 분석
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' 멤버에 액세스 할 수 없습니까? 메인, 의사, imgURL 등? – Tim
그것은 나를 변수로 해석 할 수 없다는 것을 보여 주며, 이것을 바깥으로 선언하면 asynctask가 프로그램 강제 종료로 닫습니다. – mrp1nk
해결할 수없는'main' 등의 멤버일까요? 'AsyncTask'가'Activity'에서 내부 클래스로 정의되지 않으면 해결 될 수없는'drinkNameText'와 같은 UI 요소가 될 가능성이 더 높습니다. – Squonk