Android WebView shouldInterceptRequest 异步加载
来源:互联网 发布:原油手机行情分析软件 编辑:程序博客网 时间:2024/06/05 07:58
WebView 的 shouldInterceptRequest 可以让我们监听WebView发出的请求并做相应的处理,但有个严重的问题:浏览器的渲染以及资源加载都是在一个线程中,如果在shouldInterceptRequest 处理时间过长,WebView界面就会阻塞,这是非常非常不好的。
本人最近遇到个问题,用WebView做显示,里面有个别特定url的图片需要做特殊的加载处理。所以就在shouldInterceptRequest 判断url,然后用java加载,处理,最后返回给 WebView。
一开始用shouldInterceptRequest做了如下处理:
@Override public WebResourceResponse shouldInterceptRequest(WebView view, String url) { Log.i(TAG, "shouldInterceptRequest url=" + url + ";threadInfo" + Thread.currentThread()); WebResourceResponse response = null; if(url.contains("avatar.php?")){ Bitmap bitmap = ImageLoader.getInstance().loadImageSync(url); if (bitmap != null) { InputStream in = IOUtils.bitmap2InputStream(bitmap, 100); response = new WebResourceResponse("image/png", "UTF-8", in); } } return response; }
开始感觉没有什么大问题,一段时间后发现在网络环境差的情况下非常慢,要好半天加载动画才会消失(WebView加载完成时取消加载动画的),经调试发现所有 shouldInterceptRequest的执行都在同一个线程内,如果此方法内处理时间长,不能及时返回结果,就会造成界面长时间无响应的情况, 真是万万没想到。。。
改成异步加载即可:
@Overridepublic WebResourceResponse shouldInterceptRequest(WebView view, String url) { Log.i(TAG, "shouldInterceptRequest url=" + url + ";threadInfo" + Thread.currentThread()); WebResourceResponse response = null; if(url.contains("avatar.php?")){ try { final PipedOutputStream out = new PipedOutputStream(); PipedInputStream in = new PipedInputStream(out); ImageLoader.getInstance().loadImage(url, new ImageLoadingListener() { @Override public void onLoadingStarted(String s, View view) {} @Override public void onLoadingFailed(String s, View view, FailReason failReason) {} @Override public void onLoadingCancelled(String s, View view) {} @Override public void onLoadingComplete(String s, View view, Bitmap bitmap) { if (bitmap != null) { try { out.write(IOUtils.Bitmap2Bytes(bitmap)); out.close(); }catch (Exception e){ e.printStackTrace(); } } } }); response = new WebResourceResponse("image/png", "UTF-8", in); }catch (Exception e){ e.printStackTrace(); } } return response;}
转自:http://www.codeweblog.com/android-webview-shouldinterceptrequest-%E5%BC%82%E6%AD%A5%E5%8A%A0%E8%BD%BD/
在自己的程序中WebView需要手动加载图片等资源, 使用onLoadResource进行加载,发现CSS中引用的图片,在第一次加载时没有显示,改成这个后显示就没问题了,看样子shouldInterceptRequest的加载顺序比onLoadResource要早。
0 0
- Android WebView shouldInterceptRequest 异步加载
- Android WebView shouldInterceptRequest 异步加载
- Android WebView shouldInterceptRequest 异步加载 - 屠龙宝刀,点击就送,极品装备,一秒刷爆
- Android WebView 加载资源
- Android WebView 加载网页
- Android webview加载url
- android webview加载网页
- Android webview 加载本地文件
- android webView 不能加载
- Android加载网页WebView
- android Webview加载优化
- Android WebView加载空白
- android webview 加载h5
- Android,WebView加载CookieSession
- Android-WebView加载网页
- android界面加载webview
- android 加载webview网页
- Android webview加载进度条
- Android 初级知识查询(一步一步学Android)
- bzoj 1632: [Usaco2007 Feb]Lilypad Pond bfs
- selenium不能调用chrome v54 打开网页(ubuntu16.04亲测)
- android开发解决设置铃声无效问题
- Android ImageView图片自适应
- Android WebView shouldInterceptRequest 异步加载
- JSP编程2
- caffe 去掉GPU依赖
- Cocos2d-X坐标
- 关于跳槽找工作那些事
- PHP获取当前时间
- table
- react开发必不可少的.babelrc配置
- Java设计模式之——适配器模式