>  기사  >  웹 프론트엔드  >  Android WebView 获取网页数据(html)_html/css_WEB-ITnose

Android WebView 获取网页数据(html)_html/css_WEB-ITnose

WBOY
WBOY원래의
2016-06-21 09:06:131753검색

1. activity_main.layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      tools:context=".MainActivity" >        <WebView          android:id="@+id/webView"          android:layout_width="fill_parent"          android:layout_height="fill_parent"          android:layout_centerHorizontal="true"          android:layout_centerVertical="true"           />    </RelativeLayout>



2. MainActivity.java

package com.example.webviewhtml;import android.app.Activity;  import android.graphics.Bitmap;  import android.os.Bundle;  import android.webkit.JavascriptInterface;import android.webkit.WebView;  import android.webkit.WebViewClient;  /**  * Dmeo说明:  * 当WebView加载网页时获取该网页中的内容.  * 参考资料:  * http://www.maxters.net/2012/02/android-webview-get-html-source/  */  public class MainActivity extends Activity {     private WebView mWebView;           @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          init();      }      private void init(){          mWebView=(WebView) findViewById(R.id.webView);          mWebView.getSettings().setJavaScriptEnabled(true);          mWebView.addJavascriptInterface(new InJavaScriptLocalObj(), "local_obj");          mWebView.getSettings().setSupportZoom(true);          mWebView.getSettings().setDomStorageEnabled(true);          mWebView.getSettings().setPluginsEnabled(true);          mWebView.requestFocus();          mWebView.getSettings().setUseWideViewPort(true);          mWebView.getSettings().setLoadWithOverviewMode(true);          mWebView.getSettings().setSupportZoom(true);          mWebView.getSettings().setBuiltInZoomControls(true);          mWebView.loadUrl("http://www.baidu.com");          mWebView.setWebViewClient(new WebViewClient() {              @Override              public void onPageStarted(WebView view, String url, Bitmap favicon) {                  super.onPageStarted(view, url, favicon);              }                @Override              public boolean shouldOverrideUrlLoading(WebView view, String url) {                  view.loadUrl(url);                  return true;              }                @Override              public void onPageFinished(WebView view, String url) {                  super.onPageFinished(view, url);                  view.loadUrl("javascript:window.local_obj.showSource('<head>'+"                          + "document.getElementsByTagName('html')[0].innerHTML+'</head>');");                }                @Override              public void onReceivedError(WebView view, int errorCode,                      String description, String failingUrl) {                  super.onReceivedError(view, errorCode, description, failingUrl);              }            });      }          /**     *  android sdk api >= 17 时需要加@JavascriptInterface     * @author fei     *     */    final class InJavaScriptLocalObj {      	@JavascriptInterface        public void showSource(String html) {              System.out.println("====>html="+html);          }      }  }

3. 在AndroidManifest.xml

增加权限:



성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.