2017-02-04 43 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