解析jsoup错误。根元素null

时间:2014-12-29 16:56:04

标签: java android parsing jsoup

我将使用jsoup解析此页面:http://www.verlata.it/eventi 在我的doInBackground()方法中:

@Override
        protected String doInBackground(Void... params) {

            try {
                errore = false;
                final Document doc = Jsoup.connect("http://www.verlata.it/eventi").timeout(30000).get();

                getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (doc != null) {
                                rootElement = doc.body().getElementById("div#container");
                            } else {
                                errore = true;
                                Log.d("errore", "errore parsing 1");
                                notFound.setVisibility(View.VISIBLE);
                                notFound.setText("Ops, something went wrong. Maybe the blog is offline or please check your connection.");

                            } 
                            if (rootElement != null) {
                                Elements elements = rootElement.getElementsByTag("div#body_content");
                                for(Element element : elements){
                                    if (!errore) {
                                    String descrizione = element.select("div").text();
                                    String titolo = element.select("h2").text();
                                    //String urldesc = element.select("h2 a").first().attr("abs:href");


                                    titoli.add(titolo);
                                    descrizioni.add(descrizione);
                                    //url.add(urldesc);
                                    } else {
                                        errore = true;
                                        break;
                                    }
                                }
                            } else {
                                errore = true;
                                Log.d("errore", "errore parsing 2");
                                notFound.setVisibility(View.VISIBLE);
                                notFound.setText("Ops, something went wrong. Maybe the blog is offline or please check your connection.");

                            }
                        }
                    });


            } catch (Exception e) {
                e.printStackTrace();
                /*Toast.makeText(FragmentThree.this, "Errore parsing",
                               Toast.LENGTH_SHORT).show();*/
                Log.d("errore", "errore parsing");
                notFound.setVisibility(View.VISIBLE);
                notFound.setText("Ops, something went wrong. Maybe the blog is offline or please check your connection.");
            }


        return null;
    }

logcat报告我:Log.d("errore", "errore parsing 2");所以这意味着rootElement为null ..怎么可能?我需要的是帖子的标题及其描述。

1 个答案:

答案 0 :(得分:1)

这一行错了:

doc.body().getElementById("div#container");

该方法只需要元素的ID,而不是整个搜索查询。

doc.body().getElementById("container");

如果您想使用完整的搜索查询,请使用:

doc.select("div#container");

同样适用于这一行:

Elements elements = rootElement.getElementsByTag("div#body_content");

应该是

Elements elements = doc.select("div#body_content");

您的代码如下所示:

final Document doc = Jsoup.connect("http://www.verlata.it/eventi").timeout(30000).get();
Element container = doc.body().getElementById("container");
Element bodyContent = doc.body().getElementById("body_content");
System.out.println(container);
System.out.println(bodyContent);