android中jsoup解析html的几个例子

1.获取百度所有链接的例子(通过ID):

Java代码 复制代码  收藏代码
  1. public class Activity01(改成你自己的Activity) extends Activity   
  2. {   
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState)   
  5.     {   
  6.         super.onCreate(savedInstanceState);   
  7.            
  8.         setContentView(R.layout.main);   
  9.         TextView tv = new TextView(this);   
  10.            
  11.         String myString = null;   
  12.         StringBuffer sff = new StringBuffer();//一定要new一个,我刚开始搞忘了,出不来。   
  13.         try  
  14.         {   
  15.             Document doc = Jsoup.connect("http://www.baidu.com").get();   
  16.             Elements links = doc.select("a[href]");   
  17.             //注意这里是Elements不是Element。同理getElementById返回Element,getElementsByClass返回时Elements   
  18.             for(Element link : links){   
  19.                 //这里没有什么好说的。   
  20.                 sff.append(link.attr("abs:href")).append("  ").append(link.text()).append(" ");   
  21.             }   
  22.             myString = sff.toString();   
  23.         }   
  24.         catch (Exception e)   
  25.         {   
  26.             myString = e.getMessage();   
  27.             e.printStackTrace();   
  28.         }   
  29.         /**//* 将信息设置到TextView */  
  30.         tv.setText(myString);   
  31.            
  32.         /**//* 将TextView显示到屏幕上 */  
  33.         this.setContentView(tv);   
  34.     }   
  35. }  
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 的新闻标题。

Java代码 复制代码  收藏代码
  1. package huxiaoan.cqu.praseHtml;   
  2.   
  3. import org.jsoup.Jsoup;   
  4. import org.jsoup.nodes.Document;   
  5. import org.jsoup.nodes.Element;   
  6. import org.jsoup.select.Elements;   
  7.   
  8. import android.app.Activity;   
  9. import android.os.Bundle;   
  10. import android.widget.TextView;   
  11.   
  12. public class HtmlActivity extends Activity {   
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {   
  15.         super.onCreate(savedInstanceState);   
  16.         setContentView(R.layout.main);   
  17.   
  18.         TextView tv = (TextView) findViewById(R.id.out);   
  19.         String myString = new String();   
  20.   
  21.         try {   
  22.             Document doc = Jsoup.connect("http://news.cqu.edu.cn").get();   
  23.             //Elements   
  24.             Elements topnews = doc.getElementsByClass("topnews");   
  25.             //Elements   
  26.             Elements links = topnews.select("a[href]");   
  27.             for (Element link : links) {   
  28.                 myString+=link.text();   
  29.                 myString+="\n";   
  30.             }   
  31.                
  32.         } catch (Exception e) {   
  33.   
  34.             myString = e.getMessage();   
  35.             e.printStackTrace();   
  36.         }   
  37.         /* 将信息设置到TextView */  
  38.         tv.setText(myString);   
  39.   
  40.     }   
  41. }  
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连续获取多个页面。即保持会话。

Java代码 复制代码  收藏代码
  1. package huxiaoan.cqu.praseHtml;   
  2.   
  3. import java.util.Iterator;   
  4. import java.util.Map;   
  5. import java.util.Map.Entry;   
  6.   
  7. import org.jsoup.Connection;   
  8. import org.jsoup.Connection.Response;   
  9. import org.jsoup.Jsoup;   
  10. import org.jsoup.nodes.Document;   
  11. import org.jsoup.nodes.Element;   
  12. import org.jsoup.select.Elements;   
  13.   
  14. import android.app.Activity;   
  15. import android.os.Bundle;   
  16. import android.widget.TextView;   
  17.   
  18. public class HtmlActivity extends Activity {   
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {   
  21.         super.onCreate(savedInstanceState);   
  22.         setContentView(R.layout.main);   
  23.   
  24.         TextView tv = (TextView) findViewById(R.id.out);   
  25.         String myString = new String();   
  26.         String sessionid = new String();   
  27.         try {   
  28.             // 登录获取sessionid   
  29.             Connection con = Jsoup.connect("http://www.jwc.cqu.edu.cn/login.asp")   
  30.                     .data("username""000")   
  31.                     .data("password""000");   
  32.             con.post();    
  33.             sessionid = con.response().cookie("ASPSESSIONIDCCSTRTQS");   
  34.             // 查询课表(利用读取到的session值,可以实现保持会话,连续请求了。)   
  35.             Connection con_query = Jsoup   
  36.                     .connect("http://www.jwc.cqu.edu.cn/PlanAndCurriculum/cour_tab_sel_stud.ASP")   
  37.                     .cookie("ASPSESSIONIDCCSTRTQS", sessionid);   
  38.             // 读取内容   
  39.             Document doc = con_query.get();   
  40.             Elements fonts = doc.getElementsByTag("b");   
  41.             for (Element font : fonts) {   
  42.                 myString += font.text();   
  43.             }   
  44.         } catch (Exception e) {   
  45.             myString = e.getMessage();   
  46.             e.printStackTrace();   
  47.         }   
  48.         /* 将信息设置到TextView */  
  49.         tv.setText(myString);   
  50.   
  51.     }   
  52. }  
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的值都读出来。

Java代码 复制代码  收藏代码
  1. Connection.Response res = Jsoup.connect("http://www.jwc.cqu.edu.cn/login.asp")   
  2.         .data("username""000","password""000")   
  3.         .method(Method.POST)   
  4.         .execute();   
  5. Map<String, String> cookies = res.cookies();   
  6. //如果需要 Document doc1 = res.parse();   
  7. Connection connection = Jsoup.connect("http://www.jwc.cqu.edu.cn/PlanAndCurriculum/cour_tab_sel_stud.ASP");   
  8. for (Entry<String, String> cookie : cookies.entrySet()) {   
  9.     connection.cookie(cookie.getKey(), cookie.getValue());   
  10. }   
  11. Document doc = connection.get();  
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();

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值