Android WebView获取网页数据
Android WebView获取网页数据
CrazyManDF 发表于5个月前
Android WebView获取网页数据
  • 发表于 5个月前
  • 阅读 47
  • 收藏 1
  • 点赞 0
  • 评论 0

腾讯云实验室 1小时搭建人工智能应用,让技术更容易入门 免费体验 >>>   

##Android WebView获取网页数据##

/**
 * webview加载网页url
 */
private void LoadAuthPageUrl() {

    String url = "xxxxxx";

    wbAuth.setWebChromeClient(new WebChromeClient(){

        [@Override](https://my.oschina.net/u/1162528)
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            Log.d(TAG, "==" + title);
        }
    });
    wbAuth.setWebViewClient(new WebViewClient(){

        [@Override](https://my.oschina.net/u/1162528)
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
			view.loadUrl("javascript:(function() { " + jsFormInjectCode + "})()"); //注入javascript
        }

        [@Override](https://my.oschina.net/u/1162528)
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url); 
            return true;
        }

        [@Override](https://my.oschina.net/u/1162528)
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            Log.d(TAG, "==" +errorCode + "=" + description + "=" +failingUrl);
        }
    });
	// 将FormDataInterface类中的processFormData方法映射到javascript中的window.FORMOUT.processFormData(data);
	wbAuth.addJavascriptInterface(new FormDataInterface(), "FORMOUT"); 
    WebSettings settings = wbAuth.getSettings();
    settings.setJavaScriptEnabled(true);  // 支持javascript
    settings.setDefaultTextEncodingName("utf-8");
    wbAuth.loadUrl(url); // 加载网页url
	//wbAuth.loadDataWithBaseURL(url, data,  "text/html", "utf-8", null); // 直接加载html字符串,第一个url是html种资源获取的根路径
}


 /**
 * javascript的交互类
 */
private class FormDataInterface {

    @JavascriptInterface
    public void processFormData(String formData) {
        // 获取到的值:
        // method=post&action=xx&response_type=code&redirect_uri=xxx&username=xxx&password=xxx
        Log.d(TAG, "====" + formData);
        
    }

}

// 注入的javascript代码

String jsFormInjectCode =
    "function parseForm(event) {" +
    " var form = this;" +
    " if (this.tagName.toLowerCase() != 'form')" +
    "   form = this.form;" +
    " var data = '';" +
    " if (!form.method) form.method = 'get';" +
    " data += 'method=' + form.method;" +
    " data += '&action=' + form.action;" +
    " var inputs = document.forms[0].getElementsByTagName('input');" +
    " for (var i = 0; i < inputs.length; i++) {" +
    "   var field = inputs[i];" +
    "   if (field.type != 'submit' && field.type != 'reset' && field.type != 'button')" +
    "       data += '&' + field.name + '=' + field.value;" +
    " }" +
    " window.FORMOUT.processFormData(data);" +
    "}" +
    "" +
    "for (var form_idx = 0; form_idx < document.forms.length; ++form_idx)" +
    "       document.forms[form_idx].addEventListener('submit', parseForm, false);" +
    "var inputs = document.getElementsByTagName('input');" +
    "for (var i = 0; i < inputs.length; i++) {" +
    "       if (inputs[i].getAttribute('type') == 'button')" +
    "           inputs[i].addEventListener('click', parseForm, false);" +
    "}";

[参照]https://gist.github.com/adrobisch/734117

共有 人打赏支持
粉丝 4
博文 61
码字总数 23547
×
CrazyManDF
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: