2017-02-04 44 views
0

我试图让网站的标题,如果字符串包含标题代码检查是不是“空”。如果它不为空,它将执行其余的代码。Jsoup不会分析我的网站

try { 
    Document htmlDocument = Jsoup.connect("http://afterlifesolutions.com/theaterstore/app-featured.php").userAgent("Mozilla").get(); 
    htmlContentInString = htmlDocument.title(); 
    if(htmlContentInString != null) { 
     isNull = false; 
      } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

问题是

htmlContentInString = htmlDocument.title(); 
if(htmlContentInString != null) 

应用程序将跳过 'if语句',因为它没有收到称号。但是代码在其他网站上完美运行。

+0

并在html有丝毫不差? – efekctive

+0

你记录了htmlDocument,也许你没有收到任何东西? –

+0

@efekctive这是我试图从http://afterlifesolutions.com/theaterstore/app-featured.php获取数据的网站,是的,它具有标题标签 – Afterlife

回答

0

我不知道这事,你想,但我认为这可能会帮助你。我有一些jsoup项目与自定义适配器和列表视图,所以我根据您的需要调整它,我得到了这个。基本上我想告诉你如何用jsoup解析你的html。我使用的AsyncTask摆脱URL数据,你应该使用:如果你需要它

try { 
      document = Jsoup.connect("http://afterlifesolutions.com/theaterstore/app-featured.php").get(); 
      Elements links = document.getElementsByClass("single-product"); 
      for (Element element : links) { 
       Data data = new Data(); //class where I set and get the data, of course you don't have to follow this patern 

       Elements getLink = element.select("a.product_link[href]"); 
       String link = getLink.attr("abs:href"); 
       Elements getImage = element.select("img.product_poster[src]"); 
       String image = getImage.attr("abs:src"); 
       String title = element.select("p.product_name").text(); 
       String price = element.select("p.product_price").text(); 
       Log.d(image, title); 
       data.setUrl(link); 
       data.setTopic(title); 
       data.setBitmap(getBitmap(image)); 
       datas.add(data); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

方法下载图像:

public Bitmap getBitmap(String src) 
{ 
    try { 
     URL url = new URL(src); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream inputStream = connection.getInputStream(); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = 2; 
     Bitmap myBitmap = BitmapFactory.decodeStream(inputStream, null, options); 
     return myBitmap; 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
     return null; 
    } 
} 

这是结果:

enter image description here

+0

该代码完美地完成了这项工作,我不得不在循环内添加'isNull = false'来检查它是否包含数据。谢谢 – Afterlife

+0

@Afterlife欢迎您。 – Yupi