WebView Jsoup的使用封装
来源:互联网 发布:filmimpact插件 mac版 编辑:程序博客网 时间:2024/06/06 02:29
WebView直接加载url,但是当加载的是string,带有html标签的时候,需要处理图片,甚至文字大小也不能正常显示。下面的类是在项目中遇到时做的封装。处理图片适应,文字大小适应等问题。
public interface IJsoupManager { Document getJsoupDocument(); IJsoupManager jsoupReceiverHtml(String htmlContent); String replaceHtmlImgLink() throws Exception; String getLoadImgAddress(); String doLoadHtmlContentByFontAttr() throws Exception;}
接口的实现类:
public class ClubJsoupImpl implements IJsoupManager{ private IJsoupManager mIJsoupManager; private Document doc; public ClubJsoupImpl createDecorator(IJsoupManager iJsoupManager){ this.mIJsoupManager = iJsoupManager; return this; } @Override public Document getJsoupDocument() { return doc; } @Override public IJsoupManager jsoupReceiverHtml(String htmlContent) { mIJsoupManager.jsoupReceiverHtml(htmlContent); doc = mIJsoupManager.getJsoupDocument(); return this; } @Override public String replaceHtmlImgLink() throws Exception{// 图片适应 if (mIJsoupManager == null){ new Throwable("mIJsoupManager is null, please check"); } String zHtml = mIJsoupManager.replaceHtmlImgLink(); Document doc = Jsoup.parse(zHtml.replace("\n", "").replace("\r", "")); Elements elements = doc.getElementsByTag("img"); for (Element element : elements) { element.attr("width","100%").attr("height","auto"); } return doc.toString(); } @Override public String getLoadImgAddress() { return LoadUrlUtils.pictureUrl; } @Override public String doLoadHtmlContentByFontAttr() throws Exception{ if (mIJsoupManager == null){ new Throwable("mIJsoupManager is null, please check"); } return mIJsoupManager.doLoadHtmlContentByFontAttr(); }}
下面是两种使用类
public class JsoupManager { private IJsoupManager instanceImpl; public String obtainHtmlContentByDeal(String prevHtmlParam) throws Exception { instanceImpl = new JsonManagerImpl().jsoupReceiverHtml(prevHtmlParam); return instanceImpl.replaceHtmlImgLink(); } /** * -移植抚仙湖项目的实现处理-也是把那边的html字符串数据作为基本处理方式 * -扩展时可以继承此类或者采用本项目装饰器模式进行调用实现具体内容 */ public static class JsonManagerImpl implements IJsoupManager{ protected String htmlContent; protected Document doc; public JsonManagerImpl(){ } @Override public Document getJsoupDocument() { return doc; } @Override public IJsoupManager jsoupReceiverHtml(String htmlContent) { this.htmlContent = htmlContent; doc = Jsoup.parse(htmlContent.replace("\n", "").replace("\r", "")); return this; } @Override public String replaceHtmlImgLink() throws Exception{ String html = null; if (doc == null){ new Throwable("Jsoup Document is not null, please execute jsoupReceiverHtml() to new instance for Document Object"); } Elements pngs = doc.select("img[src]"); for (Element element : pngs) { String imgUrl = element.attr("src"); if (imgUrl.trim().startsWith("/")) { if (getLoadImgAddress().equals("")){ new Throwable("please override function getLoadImgAddress(), promise it's value is not null or not \"\""); } imgUrl = getLoadImgAddress() + imgUrl; element.attr("src", imgUrl); } } html = doc.toString(); return html; } @Override public String getLoadImgAddress() { return ""; } @Override public String doLoadHtmlContentByFontAttr() throws Exception{ String html = null; if (doc == null){ new Throwable("Jsoup Document is not null, please execute jsoupReceiverHtml() to new instance for Document Object"); } Elements fonts = doc.select("span[style]"); for (Element element : fonts) { String fontSize = element.attr("style"); if (fontSize.trim().contains("font-size")) { fontSize = "font-size:48px;"; element.attr("style", fontSize); } } return html.toString(); } }}
*****************************************************************************
public class HtmlManager { /** * webview数据 * @param originalHtmlContent * @return */ public static String createHtmlContent(String originalHtmlContent){ IJsoupManager implIJsoupManager = new JsoupSrc().jsoupReceiverHtml(originalHtmlContent); String htmlLoadStr = null; try { htmlLoadStr = implIJsoupManager.replaceHtmlImgLink(); } catch (Exception e) { e.printStackTrace(); } return htmlLoadStr; } private static class JsoupSrc extends JsoupManager.JsonManagerImpl { @Override public String getLoadImgAddress() { return LoadUrlUtils.pictureUrl; } }}
阅读全文
0 0
- WebView Jsoup的使用封装
- 封装好的webview
- Jsoup的使用
- 11、Jsoup的使用
- Jsoup的简单使用
- Jsoup的使用
- Jsoup xpath的使用
- jsoup的使用
- Jsoup的简单使用
- Jsoup使用的坑
- Jsoup的使用
- Jsoup的使用
- Jsoup的使用
- webview jsoup解析html
- webview jsoup解析html
- 关于WebView的简易封装
- android 一个封装的webview
- 使用jsoup的几点小总结
- (VS2010)MFC常用语句
- logback 启动警告提醒,日志不起作用
- Linux线程死锁
- 并行计算与分布式处理的区别
- JavaWeb之El表达式
- WebView Jsoup的使用封装
- 【Maven学习(七)】----基于Mybatis-Generator自动生成Dao、Model、Mapping文件
- 线程和同步
- Error Installing APKs
- React页面登录之后返回之前浏览页面
- 26、27、28
- 获取屏幕的分辨率,工作区宽高
- JavaWeb之JSTL标签库
- 函数节流与防抖