【问题标题】:AsyncTask and JSOUP ParsingAsyncTask 和 JSOUP 解析
【发布时间】: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


【解决方案1】:

这将导致NullPointerException 开始......

@Override
protected void onPreExecute() {

    dialog = dialog.show(Result.this, " ",
            " Loading. Please wait ... ", true);
}

您不能调用dialog.show(...),因为dialog 将是null。

改为调用静态ProgressDialog.show(...) 方法。

【讨论】:

  • 好的,部分问题已解决 - 进度对话框出现,但过了一会儿它消失了,应用程序导致强制关闭,之后我在 eclipse 中看不到 logcat
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-11
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多