andriod之应用内置浏览器 webview

参考: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://stackoverflow.com/questions/18057624/two-way-sync-for-cookies-between-httpurlconnection-java-net-cookiemanager-and

转载于:https://www.cnblogs.com/fanglove/p/3502091.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值