2014-11-21 83 views
-1

我在我的android应用程序中有一个jsoup代码,用于检查网站上是否有div,但它使我返回“porukeDiv IS NOT NULL”,即使它是。下面的代码:为什么Jsoup div如果为空则不会给出“null”?

  Document doc = Jsoup.parse(html); 

      // Now you can, for example, retrieve a div with id="username" here 
      Elements porukeDiv = doc.select("div#logovan > a[header-pvtmsg-link]"); //bilo Element 
      String porukeText = porukeDiv.text(); 
      System.out.println("porukeDiv: " + porukeDiv); 
      System.out.println("porukeText: " + porukeText); 
      if (porukeDiv != null) { 
       System.out.println("porukeDiv NOT NULL! "); 
       if (porukeText.startsWith("Poruke (")) { 
        loggedin[0] = true; 
        startstopAlarmPoruke(); 
       } 
      }else{ 
       loggedin[0] = false; 
       startstopAlarmPoruke(); 
       System.out.println("porukeDiv NULL! "); 

但是,当它为空,这是信息,我得到的logcat:

11-21 06:14:25.092  868-927/com.package.pack I/System.out﹕ porukeDiv: 
11-21 06:14:25.092  868-927/com.package.pack I/System.out﹕ porukeText: 
11-21 06:14:25.092  868-927/com.package.pack I/System.out﹕ porukeDiv NOT NULL! 

为什么出现这种情况?

+0

这definitelly不为空。如果是这样,'String porukeText = porukeDiv.text();'会抛出NPE。 – 2014-11-21 11:23:28

回答

0

the API docdoc.select(...)不返回NULL,但如果没有被发现,选择相匹配的空Elements集合。

尝试测试这样说:

if (!porukeDiv.isEmpty()) { 
    System.out.println("porukeDiv NOT Empty! "); 
}else{ 
    System.out.println("porukeDiv Empty! "); 
} 
0

该对象不为空,它用空值初始化。

您可以检查porukeDiv和porukeText代替“”。

使用porukeText.equals(“”)而不是“==”操作

相关问题