2014-12-29 74 views
0

我会jsoup解析此页:http://www.verlata.it/eventi 并在此我doInBackground()方法:解析jsoup错误。根元素空

@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的是空..怎么可能?我需要的是帖子的标题及其描述。

回答

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); 
+0

MMH我试过,但我得到了同样的错误......我不知道为什么..也许错的ID? –

+0

@ End.Game你用'doc.select'试过了吗? – BackSlash

+0

以及我认为这个网站有问题..“描述”是一个div ..没有任何id ..我可以解析它吗? –