当前位置: 代码迷 >> JavaScript >> 在android中施用jsoup解析页面链接
  详细解决方案

在android中施用jsoup解析页面链接

热度:434   发布时间:2012-08-30 09:55:54.0
在android中使用jsoup解析页面链接

使用了html解析器jsoup来获取页面所有链接,顺便试下把jsoup放进android中使用下,发现jsoup在android中能直接使用,测试例子代码(本文只作了简单测试):
tt.jsp页面

<% @?page?language = " java " ?contentType = " text/html;?charset=UTF-8 "
????pageEncoding
= " UTF-8 "
%>
<! DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd" >
< html >
< head >
< meta? http-equiv ="Content-Type" ?content ="text/html;?charset=UTF-8" >
< title > Insert?title?here </ title >
</ head >
< body >
测试连接
< a? href ="http://www.blogjava.net/pengo/" > penngo的博客 </ a >
< a? href ="http://www.blogjava.net/" > blogjava博客 </ a >
</ body >
</ html >


Activity01.java

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 ?Activity01? 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();
????????
try
????????
{
????????????Document?doc?
= ?Jsoup.connect( " http://192.168.64.9:8099/AgentJava/tt.jsp " ).get();
????????????Elements?links?
= ?doc.select( " a[href] " );
????????????
for (Element?link?:?links) {
????????????????sff.append(link.attr(
" abs:href " )).append( " ?? " ).append(link.text()).append( " \n " );
????????????}

????????????myString?
= ?sff.toString();
????????}

????????
catch ?(Exception?e)
????????
{

????????????myString?
= ?e.getMessage();
????????????e.printStackTrace();
????????}

????????
/* ?将信息设置到TextView? */
????????tv.setText(myString);
????????
????????
/* ?将TextView显示到屏幕上? */
????????
this .setContentView(tv);
????}

}


运行效果:

本文是在android2.2中开发测试,jsoup在android上使用和在jdk上使用是一样,不用改变代码。

  相关解决方案