在Android中使用Jsoup时出现doInBackground错误

时间:2014-07-24 20:15:40

标签: android jsoup

我正在尝试学习Android,当我尝试使用Jsoup解析HTML时,我收到错误,我的应用停止了。我的布局中有一个按钮,当我点击它时,我希望应用程序更新TextView并编写HTML页面标题。这是代码(必要部分):

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        new Title().execute(new String[] {url}); // I have url of an html address as a string above
    }
}

class Title extends AsyncTask <String, Void, String> {

    String pageTitle;

    protected String doInBackground(String... params) {

        try {
            Document doc = Jsoup.connect(params[0]).get();
            pageTitle = doc.title();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return pageTitle;
    }

    protected void onPostExecute (String... params) {
        MainActivity.title.setText(params[0]);
    }

}

当我点击按钮时,它会显示&#34;不幸的是,App已停止。&#34;

有谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

在doInBackground()中尝试:

    { 
        String pageTitle;
        Element ele;
            protected String doInBackground(String... params) {

                try {
                    Document doc = Jsoup.connect(params[0]).get();
                    t =doc.select("title");
                    pageTitle = t.text();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
           return pageTitle;
     }