2015-11-17 41 views
1

嗨,我已經在這個問題上幾天了。作爲項目的一部分,我需要使用JSOUP將這個eBuyer Search網站的產品名稱和價格返回到我的應用程序中。從網站上颳去信息並列入清單?

我想知道3個問題,在一分鐘的代碼帶回產品名稱和該頁面上的所有價格作爲一個句子的所有H1頭。

  1. 有沒有一種方法可以解析Android中的信息,一次帶回一個項目並列出它,以及產品名稱而不是文本塊。
  2. 一旦正確的信息被列出,我將如何傾聽對該產品的特定點擊並將其存儲爲變量?

非常感謝你的幫助,上述

private class Title extends AsyncTask<Void, Void, Void> { 

    String h1,h3; 

    @Override 
    protected Void doInBackground(Void... params) { 
     try { 
      // Connect to the web site 
      Document element = Jsoup.connect("http://www.ebuyer.com/search?q=" + search).get(); 

      h1 = element.body().getElementsByTag("h2").text(); 

      h3 = element.body().getElementsByTag("h1").text(); 

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

     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // Set title into TextView 
     TextView textView = (TextView) findViewById(R.id.textView3); 
     textView.setText(h3); 

     TextView textView2 = (TextView) findViewById(R.id.textView2); 
     textView2.setText(h1); 
    } 
} 

代碼是我使用JSOUP

圖片下面的方法是,當我搜索一個產品會發生什麼。所有的

enter image description here

回答

1

這裏是代碼即可獲得每個產品

Document doc = Jsoup.connect("http://www.ebuyer.com/search?q=" + search).timeout(10000).userAgent("Mozilla/5.0").get(); 
Elements sections = doc.select("div.listing-product"); 
for (Element section : sections) { 
    String title = section.select("h3.listing-product-title").text(); 
    String price = section.select("p.price").text(); 
    System.out.println("Title : " + title); 
    System.out.println("Price : " + price); 
} 

現在使用列表視圖來顯示每個產品時,列表項的點擊是指當選擇的產品,你可以做任何你想要的名稱和價格。
你可以瞭解列表視圖從http://www.vogella.com/tutorials/AndroidListView/article.html
http://developer.android.com/guide/topics/ui/layout/listview.html

+0

謝謝你試試這個。 – Rueben

0

首先,你可以微調元素的目標。在您提到的頁面上,每個產品都包含在類別爲listing-product的元素中。在此範圍內,標題由類別listing-product-title指定,而價格位於類別爲listing-price的元素中。

其次,getElementsBy...方法返回一個Elements對象,其實質上是所有匹配的ArrayList。您應該遍歷列表並單獨處理每個項目。

例子:

Document element = Jsoup.connect("http://www.ebuyer.com/search?q=" + search).get(); 
Elements products = element.body().getElementsByClass("listing-product"); 
for(Element product : products){ 
    String title = product.getElementsByClass("listing-product-title").text(); 
    String price = product.getElementsByClass("listing-product-price").text(); 
} 

你的項目做的是給你。我會創建一個POJO類來保存您的產品數據,並將所有產品添加到ArrayList。然後,您可以使用該列表來備份適配器,以獲得ListViewGridView或其他東西。