1.獲取百度所有鏈接的例子(通過ID):
- public class Activity01(改成你自己的Activity) extends Activity
- {
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TextView tv = new TextView(this);
- String myString = null;
- StringBuffer sff = new StringBuffer();//一定要new一個,我剛開始搞忘了,出不來。
- try
- {
- Document doc = Jsoup.connect("http://www.baidu.com").get();
- Elements links = doc.select("a[href]");
- //注意這裏是Elements不是Element。同理getElementById返回Element,getElementsByClass返回時Elements
- for(Element link : links){
- //這裏沒有什麼好說的。
- sff.append(link.attr("abs:href")).append(" ").append(link.text()).append(" ");
- }
- myString = sff.toString();
- }
- catch (Exception e)
- {
- myString = e.getMessage();
- e.printStackTrace();
- }
- /**//* 將信息設置到TextView */
- tv.setText(myString);
- /**//* 將TextView顯示到屏幕上 */
- this.setContentView(tv);
- }
- }
2.獲取news.cqu.edu.cn中class爲topnews 的新聞標題。
- package huxiaoan.cqu.praseHtml;
- 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.widget.TextView;
- public class HtmlActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TextView tv = (TextView) findViewById(R.id.out);
- String myString = new String();
- try {
- Document doc = Jsoup.connect("http://news.cqu.edu.cn").get();
- //Elements
- Elements topnews = doc.getElementsByClass("topnews");
- //Elements
- Elements links = topnews.select("a[href]");
- for (Element link : links) {
- myString+=link.text();
- myString+="\n";
- }
- } catch (Exception e) {
- myString = e.getMessage();
- e.printStackTrace();
- }
- /* 將信息設置到TextView */
- tv.setText(myString);
- }
- }
3.利用session連續獲取多個頁面。即保持會話。
- package huxiaoan.cqu.praseHtml;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Map.Entry;
- import org.jsoup.Connection;
- import org.jsoup.Connection.Response;
- 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.widget.TextView;
- public class HtmlActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TextView tv = (TextView) findViewById(R.id.out);
- String myString = new String();
- String sessionid = new String();
- try {
- // 登錄獲取sessionid
- Connection con = Jsoup.connect("http://www.jwc.cqu.edu.cn/login.asp")
- .data("username", "000")
- .data("password", "000");
- con.post();
- sessionid = con.response().cookie("ASPSESSIONIDCCSTRTQS");
- // 查詢課表(利用讀取到的session值,可以實現保持會話,連續請求了。)
- Connection con_query = Jsoup
- .connect("http://www.jwc.cqu.edu.cn/PlanAndCurriculum/cour_tab_sel_stud.ASP")
- .cookie("ASPSESSIONIDCCSTRTQS", sessionid);
- // 讀取內容
- Document doc = con_query.get();
- Elements fonts = doc.getElementsByTag("b");
- for (Element font : fonts) {
- myString += font.text();
- }
- } catch (Exception e) {
- myString = e.getMessage();
- e.printStackTrace();
- }
- /* 將信息設置到TextView */
- tv.setText(myString);
- }
- }
這個例子經過我無數次的測試,經常出現讀不到session值的情況。耽誤了我很長一段時間。
找了各種英文網站,找到了一種解決辦法,我不知道以後還會不會出現問題 。解決方法是,把所有cookie的值都讀出來。
- Connection.Response res = Jsoup.connect("http://www.jwc.cqu.edu.cn/login.asp")
- .data("username", "000","password", "000")
- .method(Method.POST)
- .execute();
- Map<String, String> cookies = res.cookies();
- //如果需要 Document doc1 = res.parse();
- Connection connection = Jsoup.connect("http://www.jwc.cqu.edu.cn/PlanAndCurriculum/cour_tab_sel_stud.ASP");
- for (Entry<String, String> cookie : cookies.entrySet()) {
- connection.cookie(cookie.getKey(), cookie.getValue());
- }
- Document doc = connection.get();