Android的jsoup方法,android中jsoup解析html的几个例子

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 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 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、付费专栏及课程。

余额充值