配置WebView
来源:互联网 发布:保定安全教育网络平台 编辑:程序博客网 时间:2024/06/05 09:50
一、基本配置
1、wb.setWebChromeClient(new WebChromeClient());//用于加载javascript
//setWebChromeClient()源码/** * Sets the chrome handler. This is an implementation of WebChromeClient for * use in handling JavaScript dialogs, favicons, titles, and the progress. * This will replace the current handler. * * @param client an implementation of WebChromeClient */ public void setWebChromeClient(WebChromeClient client) {}
2、wb.setWebViewClient(new WebViewClient()//用于接收通知和请求的,可重写方法实现功能
//比如:wb.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (TextUtils.isEmpty(url)) { view.loadUrl(url);//在同一webview跳转网页 } return true; } });`
//setWebClient()源码/** * Sets the WebViewClient that will receive various notifications and * requests. This will replace the current handler. * * @param client an implementation of WebViewClient */ public void setWebViewClient(WebViewClient client) {}
3、WebSettings ws = wb.getSettings();
ws.setJavaScriptEnabled(true);//webview允许执行js4、ws.setBuiltInZoomControls(false);// 隐藏缩放按钮
5、ws.setSaveFormData(false);// 保存表单数据
6、ws.setGeolocationEnabled(true);// 启用地理定位
7、ws.setAppCacheEnabled(false);//是否允许缓存
8、 ws.setAppCacheMaxSize(1024 * 10);//设置缓存大小
9、ws.setUseWideViewPort(true);// 可任意比例缩放
10、ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);//可能的话使所有列的宽度不超过屏幕宽度
//为实现网页适配各类手机,pad,除了网页前端处理好,android这边也要处理。 第9个方法必须为true的情况下,所设置的适配方法才能有效。
1、LayoutAlgorithm.NARROW_COLUMNS :适应内容大小
2、LayoutAlgorithm.SINGLE_COLUMN:适应屏幕,内容将自动缩放
二、上传参数
1、loadUrl(String url, Map<String, String> additionalHttpHeaders);//可传header2、postUrl(String url, byte[] postData)//可传参数
但是这两个方法不能同时使用。
//postUrl源码/** * Loads the URL with postData using "POST" method into this WebView. If url * is not a network URL, it will be loaded with {@link #loadUrl(String)} * instead, ignoring the postData param. * * @param url the URL of the resource to load * @param postData the data will be passed to "POST" request, which must be * be "application/x-www-form-urlencoded" encoded. */ public void postUrl(String url, byte[] postData) { checkThread(); if (URLUtil.isNetworkUrl(url)) { mProvider.postUrl(url, postData); } else { mProvider.loadUrl(url); } }
//loadUrl源码/** * Loads the given URL with the specified additional HTTP headers. * * @param url the URL of the resource to load * @param additionalHttpHeaders the additional headers to be used in the * HTTP request for this URL, specified as a map from name to * value. Note that if this map contains any of the headers * that are set by default by this WebView, such as those * controlling caching, accept types or the User-Agent, their * values may be overriden by this WebView's defaults. */ public void loadUrl(String url, Map<String, String> additionalHttpHeaders) { checkThread(); mProvider.loadUrl(url, additionalHttpHeaders); }
3、既传header又传参数
需重写webClient
public class CustomWebClient extends WebViewClient { String header = ""; public CustomWebClient(Activity activity) { header = Utility.getUserAgent(activity); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { String actionId = ""; String path = request.getUrl().toString(); if (path.contains(Constants.mPrefix_clocks)) { actionId = getValueByName(path, "action");//Uri.parse(path).getQueryParameter("action"); String mParams = ""; try { URL mUrl = new URL(path); HttpURLConnection connection = (HttpURLConnection) mUrl.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//application/Json; charset=UTF-8 connection.setRequestProperty("User-Agent", Constants.USER_AGENT_PREFIX + header);// connection.setRequestProperty("actionid", URLEncoder.encode(actionId);//参数一// connection.setRequestProperty("secretString", URLEncoder.encode(secretString));//参数二 mParams = "actionid=" + URLEncoder.encode(actionId) + "&secretString=" + URLEncoder.encode(RequestParams.encryptStr1(actionId)); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); //建立输入流,向指向的URL传入参数 DataOutputStream os = new DataOutputStream(connection.getOutputStream());//getoutputstream自动连接 os.writeBytes(mParams); os.flush(); os.close(); return new WebResourceResponse("text/html", connection.getContentEncoding(), connection.getInputStream()); } catch (Exception e) { e.printStackTrace(); } } return null; } public String getValueByName(String url, String name) { String result = ""; int index = url.indexOf("?"); String temp = url.substring(index + 1); String[] keyValue = temp.split("&"); for (String str : keyValue) { if (str.contains(name)) { result = str.replace(name + "=", ""); break; } } return result; } }
//然后:
webview.loadUrl(url);
三、关于webview缓存
@Overrideprotected void onDestroy() { super.onDestroy(); //清空所有Cookie CookieSyncManager.createInstance(QzmobileApp.getContext()); //Create a singleton CookieSyncManager within a context CookieManager cookieManager = CookieManager.getInstance(); // the singleton CookieManager instance cookieManager.removeAllCookie();// Removes all cookies. CookieSyncManager.getInstance().sync(); // forces sync manager to sync now webView.setWebChromeClient(null); webView.setWebViewClient(null); webView.getSettings().setJavaScriptEnabled(false); webView.clearCache(true);}
四、关于Content-Type 未完待续。。。
参考:
http://blog.csdn.net/liwei123liwei123/article/details/52624826
http://blog.csdn.net/u014473112/article/details/52176412
http://blog.csdn.net/mygoon/article/details/48546781
http://blog.csdn.net/u014473112/article/details/52176412
- 配置WebView
- WebView最佳配置
- Android webview 常用配置
- Https-webview配置
- WebView加载URL配置
- WebView的WebSettings配置
- android webview 配置及常见问题
- WebView的基本属性配置
- WebView加载网页基本配置
- WebView常用配置和方法
- andriod webview 配置网络权限
- android js交互备忘,webview配置
- WebView的配置和网页参数传递
- 配置好的webview 拿来就用
- WebView 的基本写法与配置
- Cordova 配置WebView可以打开外部链接
- Cordova config.xml配置WebView全屏浏览
- WebView
- php Restful设计
- CentOS上配置rsyslog客户端用以远程记录日志
- 笔记—自定义View之绘制基础
- \x 的解码
- HDU 6200 mustedge ACM/ICPC 2017 Shenyang Online(LCT动态缩点)
- 配置WebView
- redis服务器搭建
- nodejs的hook实现
- 预加载与懒加载
- 背包九讲之多重背包 Java
- HDU6201 transaction transaction transaction
- JavaScript基础之基础
- 抽象类和接口
- 常用编辑工具批量多行处理快捷键