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();                     
                    }
效果图预览:
Android:抓取网页重点内容加载到聊天框(jsoup解析网页)_第1张图片
test.png

你可能感兴趣的:(Android:抓取网页重点内容加载到聊天框(jsoup解析网页))