編輯:關於 Android 程式設計
在 Android 開發過程中,抓取、解析與展示網頁內容幾乎是難免的課題。本文就透過 jsoup 這個輕巧好用的 Java 函式庫,示範如何抓取 cnbeta.com 網站的話題分類,並把結果呈現在清單上。
由於 jsoup 的用法相當直覺,這裡就不再贅述細節,直接看主要程式碼:
package com.android.web; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.util.ByteArrayBuffer; import org.apache.http.util.EncodingUtils; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; public class _GetWebResoureActivity extends Activity { Document doc; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { load(); } }); } protected void load() { try { doc = Jsoup.parse(new URL("https://www.cnbeta.com"), 5000); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } List<Map<String, String>> list = new ArrayList<Map<String, String>>(); Elements es = doc.getElementsByClass("main_navi"); for (Element e : es) { Map<String, String> map = new HashMap<String, String>(); map.put("title", e.getElementsByTag("a").text()); map.put("href", "https://www.cnbeta.com" + e.getElementsByTag("a").attr("href")); list.add(map); } ListView listView = (ListView) findViewById(R.id.listView1); listView.setAdapter(new SimpleAdapter(this, list, android.R.layout.simple_list_item_2, new String[] { "title","href" }, new int[] { android.R.id.text1,android.R.id.text2 })); } /** * @param urlString * @return */ public String getHtmlString(String urlString) { try { URL url = null; url = new URL(urlString); URLConnection ucon = null; ucon = url.openConnection(); InputStream instr = null; instr = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(instr); ByteArrayBuffer baf = new ByteArrayBuffer(500); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } return EncodingUtils.getString(baf.toByteArray(), "gbk"); } catch (Exception e) { return ""; } } }
這裡要特別提醒的是 getElementsByClass("main_navi") 這一行——選擇器一定要對準目標元素的 class,否則抓不到正確的資料。下圖就是執行後的預覽效果:

想深入學習 Android 的讀者,不妨延伸閱讀本站相關專題:《Android 調試技巧與常見問題解決方法匯總》、《Android 開發入門與進階教程》、《Android 多媒體操作技巧匯總(音頻、視頻、錄音等)》、《Android 基本組件用法總結》、《Android 視圖 View 技巧總結》、《Android 布局 layout 技巧總結》與《Android 控件用法總結》。
希望本文的實作範例能為你的 Android 學習帶來幫助。
Android 搜索框即時顯示清空按鈕
在搜索相關頁面中,加入一鍵清空輸入內容的功能,可以提升使用者體驗,讓刪除文字更直覺。常見做法是把 EditText 與一個清空 ImageView 放在同一個相對佈局中。
Android Launcher 機制解析:添加快捷方式與桌面小部件的完整流程
研究 Launcher 模塊兩個月下來,最大的體會是:Launcher 本身其實沒做太多事,它把大量工作交给了框架與各應用程式提供的標準介面。很多乍看之下神奇的功能,讀過
Android TableLayout 表格布局詳解
用 TableLayout 管理行列排列 TableLayout 以列與欄的方式管理子控制項。每一列通常對應一個 TableRow,而 TableRow 內每加入一個子控
Android 後台消息推送方案調研全解析
前言 假設我們已經完成了一款類似天氣預報的 App(以下稱 A 應用)。現在的新需求是:當 A 應用有新版本,或天氣出現惡劣狀況時,需要及時把手機端的通知推送出去,提醒用