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();
}
把消息内容以固定格式存于本地数据库:

消息格式:<gweb url = ,img=,title=,describe=/>
以handler方式更新UI刷新;

if(rHandle != null)
    rHandle.sendEmptyMessage(IMCons.WEB_REFRESH);

//ui
 if( msg.what == IMCons.WEB_REFRESH){
    if(adapter != null)
      adapter.notifyDataSetChanged();
    tvContent.invalidate();                     
                    }
效果图预览:
test.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,279评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,671评论 4 61
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,095评论 19 139
  • 对一个人的最好评价应该莫过于靠谱吧。因为那意味着ta做事妥当,做人得体,值得信任和托付。 那么,做事靠谱应该从哪入...
    Olive_01阅读 1,225评论 0 1
  • 差不多一个星期过去了,比上次称的时候重了一斤多,身体好像有些开始厌了,坚持住,你可以更重的~ 今天喊了一下午的唤醒...
    阿立立哥阅读 1,153评论 0 0

友情链接更多精彩内容