参考:http://my.eoe.cn/694183/archive/10476.html
http://blog.csdn.net/it_ladeng/article/details/8136534
一.webView获取html页面中标签的方法:
1.在WebViewClient里的onPageFinished回调方法中执行js代码:
view.loadUrl("javascript:console.log('MAGIC'+document.getElementById('wxImg')['src']);");
2.在WebChromeClient里的onConsoleMessage回调方法中截取LOG信息:
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
if (consoleMessage.message().startsWith("MAGIC")) {
String msg = consoleMessage.message().substring(5); // strip off prefix
com.youku.util.Logger.d("dazhu_IntegratedWebView", "msg : " + msg);
if (!TextUtils.isEmpty(msg)) {
if (oritationChangeActivity != null)
oritationChangeActivity.setReceiveImage(msg);
}
return true;
}
return super.onConsoleMessage(consoleMessage);
}
二.支持缩放,电脑网页适配手机屏幕
// 支持缩放
wb.getSettings().setBuiltInZoomControls(true);
wb.getSettings().setSupportZoom(true);
// 电脑网页最小化适配手机屏幕
wb.getSettings().setUseWideViewPort(true);
wb.getSettings().setLoadWithOverviewMode(true);
三.app与webview共用cookie.前提访问网络用的是HttpURLConnection。
private void initCookie() {
android.webkit.CookieSyncManager.createInstance(this);
// unrelated, just make sure cookies are generally allowed
android.webkit.CookieManager.getInstance().setAcceptCookie(true); // magic starts here
WebkitCookieManagerProxy coreCookieManager = new WebkitCookieManagerProxy(null, java.net.CookiePolicy.ACCEPT_ALL);
java.net.CookieHandler.setDefault(coreCookieManager);
}
public class WebkitCookieManagerProxy extends CookieManager
{
private android.webkit.CookieManager webkitCookieManager; public WebkitCookieManagerProxy()
{
this(null, null);
} public WebkitCookieManagerProxy(CookieStore store, CookiePolicy cookiePolicy)
{
super(null, cookiePolicy); this.webkitCookieManager = android.webkit.CookieManager.getInstance();
} @Override
public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException
{
// make sure our args are valid
if ((uri == null) || (responseHeaders == null)) return; // save our url once
String url = uri.toString(); // go over the headers
for (String headerKey : responseHeaders.keySet())
{
// ignore headers which aren't cookie related
if ((headerKey == null) || !(headerKey.equalsIgnoreCase("Set-Cookie2") || headerKey.equalsIgnoreCase("Set-Cookie"))) continue; // process each of the headers
for (String headerValue : responseHeaders.get(headerKey))
{
this.webkitCookieManager.setCookie(url, headerValue);
}
}
} @Override
public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException
{
// make sure our args are valid
if ((uri == null) || (requestHeaders == null)) throw new IllegalArgumentException("Argument is null"); // save our url once
String url = uri.toString(); // prepare our response
Map<String, List<String>> res = new java.util.HashMap<String, List<String>>(); // get the cookie
String cookie = this.webkitCookieManager.getCookie(url); // return it
if (cookie != null) res.put("Cookie", Arrays.asList(cookie));
return res;
} @Override
public CookieStore getCookieStore()
{
// we don't want anyone to work with this cookie store directly
throw new UnsupportedOperationException();
}
}
参考:http://*.com/questions/18057624/two-way-sync-for-cookies-between-httpurlconnection-java-net-cookiemanager-and