android jsoup简书,Android:抓取网页重点内容加载到聊天框(jsoup解析网页)

本文为在im程序中仿QQ的链接解析,利用 jsoup开源解析html库,提取网页的标题、描述,以及网页图片;

jsoup链接

消息内容中判断为网页时,new 线程加载网页信息:

executorService = Executors.newFixedThreadPool(8);

if(MsgType.isWeb(content))

{//解析网页内容;

executorService.submit(new WebParesThread(url, handler, gchat));

}

利用jsoup解析需要的内容:

取图片链接和标题

Document doc = Jsoup.parse(new URL(web_http), 5000);

//获取网页标题;

web_title = doc.title();

if( web_title.equals(""))

web_title = resLink;

//取href标签以png/ico/jpg结尾的链接;

Elements png_eles;

String reg = "http://.*\\.(jpg|png|ico)";

if( ( png_eles = doc.getElementsByAttributeValueMatching("href", reg) ) != null

&& !png_eles.isEmpty()){

Element png_ele = png_eles.first();

web_icon_link = png_ele.attr("href");

}else if(( png_eles = doc.getElementsByAttributeValueMatching("content", reg) ) != null

&& !png_eles.isEmpty()){

Element png_ele = png_eles.first();

web_icon_link = png_ele.attr("content");

}else if(( png_eles = doc.getElementsByAttributeValueMatching("src", reg) ) != null

&& !png_eles.isEmpty()){

Element png_ele = png_eles.first();

web_icon_link = png_ele.attr("src");

}

取描述文件

//取meta标签description 内容;

Elements meta_elements = doc.head().select("meta");

for (Element meta_e : meta_elements) {

if ("description".equalsIgnoreCase(meta_e.attr("name"))) {

web_description = meta_e.attr("content");

break;

} else if("description".equalsIgnoreCase(meta_e.attr("itemprop")) ){

web_description = meta_e.attr("content");

break;

}

}

以http方式下载网页中的图片链接:

HttpGet get = new HttpGet(web_icon_link);

//修改org.apache.http的主机名验证解决问题。

SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());

try

{

file.createNewFile();

HttpResponse response = httpClient.execute(get);

if (response.getStatusLine().getStatusCode() == 200)

{

InputStream input = response.getEntity().getContent();

FileOutputStream out = new FileOutputStream(file);

byte[] buffer = new byte[1024];

int len = 0;

while ((len = input.read(buffer)) != -1)

{

out.write(buffer, 0, len);

}

input.close();

out.close();

String fileIShowPath = file.getAbsolutePath();

bitmap = ImageUtil.decodeImageFile(fileIShowPath, imageWidth,imageHeight,ImageUtil.IMAGE_DECODE_TYPE_PIXELS);

bitmap = ImageUtil.toRoundCorner(bitmap, 17);

if (bitmap == null)

{

return;

}

replaceTo = "file:" + fileIShowPath;

addBitmapToMemoryCache(replaceTo, bitmap);

}catch (ClientProtocolException e)

{

e.printStackTrace();

}

catch (IOException e)

{

e.printStackTrace();

}

把消息内容以固定格式存于本地数据库:

消息格式:

以handler方式更新UI刷新;

if(rHandle != null)

rHandle.sendEmptyMessage(IMCons.WEB_REFRESH);

//ui

if( msg.what == IMCons.WEB_REFRESH){

if(adapter != null)

adapter.notifyDataSetChanged();

tvContent.invalidate();

}

效果图预览:

c2e0b736946e

test.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值