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);//可传header
2、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 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);
@Override
protected 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);
}
参考:
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