AndroidはWebViewでウェブソースを取得します.

7138 ワード

1.イネーブルjavascript:

1webView.getSettings().setJavaScriptEnabled(true);2.ローカルインターフェースの作成

1
2
3
4
5finalclassInJavaScriptLocalObj {    publicvoidshowSource(String html) {        Log.d("HTML", html);    }}3.ウェブページにローカルインターフェースを暴露する

1webView.addJavascriptInterface(newInJavaScriptLocalObj(), "local_obj");4.自分のWebView Cientを作成し、onPageFinishedからウェブソースを抽出する.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16finalclassMyWebViewClient extendsWebViewClient{      publicbooleanshouldOverrideUrlLoading(WebView view, String url) {           view.loadUrl(url);           returntrue;           publicvoidonPageStarted(WebView view, String url, Bitmap favicon) {        Log.d("WebView","onPageStarted");        super.onPageStarted(view, url, favicon);    }        publicvoidonPageFinished(WebView view, String url) {        Log.d("WebView","onPageFinished ");        view.loadUrl("javascript:window.local_obj.showSource('<head>'+"+                "document.getElementsByTagName('html')[0].innerHTML+'</head>');");        super.onPageFinished(view, url);    }} 
結合されたコードは以下の通りです.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19.
20
21
22
23
24.
25
26
27.
28
29
30
31
32
33
34
35
36
37
38.
39
40
41
42
43
44
45
46
47packagecom.hi.briancol.htmlsource; importandroid.app.Activity;importandroid.graphics.Bitmap;importandroid.os.Bundle;importandroid.util.Log;importandroid.webkit.WebView;importandroid.webkit.WebViewClient; publicclassHtmlSource extendsActivity {    privateWebView webView;    /** Called when the activity is first created. */    @Override    publicvoidonCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        webView = (WebView)findViewById(R.id.webview);        webView.getSettings().setJavaScriptEnabled(true);        webView.addJavascriptInterface(newInJavaScriptLocalObj(), "local_obj");        webView.setWebViewClient(newMyWebViewClient());        webView.loadUrl("http://www.cnblogs.com/hibraincol/");    }             finalclassMyWebViewClient extendsWebViewClient{          publicbooleanshouldOverrideUrlLoading(WebView view, String url) {               view.loadUrl(url);               returntrue;                   publicvoidonPageStarted(WebView view, String url, Bitmap favicon) {            Log.d("WebView","onPageStarted");            super.onPageStarted(view, url, favicon);        }            publicvoidonPageFinished(WebView view, String url) {            Log.d("WebView","onPageFinished ");            view.loadUrl("javascript:window.local_obj.showSource('<head>'+"+                    "document.getElementsByTagName('html')[0].innerHTML+'</head>');");            super.onPageFinished(view, url);        }    }         finalclassInJavaScriptLocalObj {        publicvoidshowSource(String html) {            Log.d("HTML", html);        }    }}ポイントは:
view.loadUrl(「javascript:window.local.show Source(''+document.getElements ByTagName(')[0].innerHTML+'')));)
実行して、showSource(String)の中でウェブページのソースコードを印刷したことが見えます.