Android WebView

来源:互联网 发布:欧洲卡车模拟2mac安装 编辑:程序博客网 时间:2024/04/27 22:00

今天 遇到一个问题 设置webView加载完页面之后要滚到页面的地底部,要实现这个功能要使用ScrollTo(int x,int y)是我们要便宜到的xy坐标,WebView中有两个获得高度的方法getHeight()这个是得到View的高度 得到的是像素,而getContentHeight()api上说明是你的HTml的高度,但是没说是什么单位,今天用到的时候发现这个是dp的单位,所以我们只需要将getContentHeight()得到的值乘上你的设备而的密度(getResources().getDisplayMetrics().density)在减去getHeight()得到的像素就是webview要便宜的y值了--------------(int)(webview.getContentHeight()*density)-webview.getView();,这样你调用scrollTo(0,(int)(webview.getContentHeight()*density)-webview.getView());

之后发现 并没什么反应,经过查看前辈的经验发现 要在webview.postDelayed(new Runnable(){public void run() {scrollTo()},300);这样就可以了,只有当我们的界面绘制完成后scrollTo才会有效果,所以我们延迟一会后执行。

实际项目中webview的使用。几个常用的方法

webView.getSettings().setJavaScriptEnabled(true);//页面是否可以执行js,默认是不能执行
webView.getSettings().setAllowFileAccess(true);//是否启动文件系统 默认是启动的
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//指定滚动条样式
webView.getSettings().setDomStorageEnabled(true);//设置可以使用localStorage
webView.getSettings().setSavePassword(false);//是否保存密码
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);// 设置宽度适应屏幕


webView.addJavascriptInterface(new InJavaScriptLocalObj(), "local_obj");//提供页面js接口,是js能调用android的接口,第一个参数是我们提供的接口对象,第二个参数相当于要使用的接口名字

class InJavaScriptLocalObj {
@JavascriptInterface
public void showSource(String html) {
logger.e("html = " + html);

}
}

上面上我们定义的接口对象里面有js可以调用的方法,之后我们调用view.loadUrl(“javascript:local_obj.showSource()”)

PS:view.loadUrl("javascript:window.local_obj.showSource('<head>'+"
+ "document.getElementsByTagName('html')[0].innerHTML+'</head>');");//这个方法是读取页面的源代码,之后就可以在log下打印出来了


在web端要是想执行我们的方法 只要<img src="……" onclick="window.local_obj.showSouce('sdada')" />就可以这样js就可以调用androud的方法了

而android要调用js的直接要通过webView.loadUrl就可以了。

setWebViewClient()和setWebChromeClient()通常android应用做本地化和HTMl结合的项目这个是肯定要设置的。

1.setWebViewClient()设置我们自己的wbviewClient来响应和请求消息,

webView.setWebViewClient(new WebViewClient() {
//页面开始的回调方法
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);

}
//页面加载url回调方法
public boolean shouldOverrideUrlLoading(WebView view, String url) {
logger.e("over read url = " + url);
view.loadUrl(url);
return true;
}
//页面加载结束回调方法
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:window.local_obj.showSource('<head>'+"
+ "document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
//页面出错
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
//通知主机应用程序使用WebView收到HTTP身份验证请求。
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
super.onReceivedHttpAuthRequest(view, handler, host, realm);
}
});

  2setWebChromeClient()设置网页上的各种请求

ps: 今天发现了在注入js的时候 4.2以上会报 "Uncaught TypeError: Object [object Object] has no method 'showSource'", source: (1) 这个异常,查阅发现4.2之后要向暴露的接口添加@JavascriptInterface  注释。

官方文档说明:


From the Android 4.2 documentation:

Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.

详细地址http://blog.csdn.net/zgjxwl/article/details/9627685

问题

1.webview访问https的时候显示空白页面,原因:进入一个不可信的网站所以显示空白

解决方法

//当webview 访问https 的时候 身份验证没通过是调用此方法在我们WebViewClient中添加



@Override
public void onReceivedSslError(WebView view,
SslErrorHandler handler, SslError error) {
// TODO Auto-generated method stub
handler.proceed();//让验证通过
}

2.当我们前端页面使用到 Request.UrlReferrer 的时候并且用到“上一页url”的内容的时候,Request.UrlReferrer  详细解释 

:http://zh.wikipedia.org/wiki/HTTP%E5%8F%82%E7%85%A7%E4%BD%8D%E5%9D%80    或者http://stswordman.cnblogs.com/archive/2006/06/12/423910.html

会发现我们的webview会是空白一片(ps:页面出错了 IOS不会有这个问题  orz~~),要解决这个问题 1,只能是前端改变获取返回参数的方法

2.我们在加载有这种操作的时候  进行下面设置

webview 中有loadUrl(String url, Map<StringString> additionalHttpHeaders) 这个方法  :给加载的url添加附加的http header

到这里 其实我们就明白了要想使用微软的Request.UrlReferrer我们必须提供给它附加的header 

<span style="white-space:pre"></span>Map<String, String> HttpHeaders = new HashMap<String, String>();<span style="white-space:pre"></span>HttpHeaders.put("referer", loadUrl);<span style="white-space:pre"></span>view.loadUrl(loadUrl, HttpHeaders);
OK,问题解决了。

0 0
原创粉丝点击