android中webView中对HTML的一些操作【更新代码】

可通过JS脚本得到HTML的源文件。然后通过正则表达式或是Jsoup类库,分析标签

package com.cchen.webinfo;



import java.util.Arrays;
import java.util.HashMap;


import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewInfoSearcherActivity extends Activity
{
    private WebView webView;
	private WebSettings settings;
	private String abird_url;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        abird_url = this.getString(R.string.abird_url);
        
        initWeb();
        
    }

	private void initWeb() 
	{
		webView = (WebView) this.findViewById(R.id.webView);
		settings = webView.getSettings();
		settings.setLoadsImagesAutomatically(true);
		settings.setJavaScriptEnabled(true);

		
		webView.addJavascriptInterface(new JSinterface(), "JS");//JS,js`s name
		
		webView.setWebViewClient(new WebViewClient() 
		{
			
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) 
			{
				if(url.startsWith("mms"))
				{
					Log.d("url", url);
					Uri uri = Uri.parse(url);

					Intent intent = new Intent(Intent.ACTION_VIEW,uri);
					intent.addCategory(Intent.CATEGORY_BROWSABLE);
					intent.addCategory(Intent.CATEGORY_DEFAULT);
					
					WebViewInfoSearcherActivity.this.startActivity(intent);
				}
				return true;
			}
			
			@Override
			public void onPageFinished(WebView view, String url) 
			{
				view.loadUrl("javascript:window.JS.getHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');"); 
				super.onPageFinished(view, url);
			}
			
		});
		
		webView.setWebChromeClient(new WebChromeClient()
		{
			@Override
			public boolean onConsoleMessage(ConsoleMessage consoleMessage) 
			{
				System.out.println(consoleMessage.message());
				webView.reload();
				return super.onConsoleMessage(consoleMessage);
			}
		});

		webView.setOnTouchListener(new View.OnTouchListener() 
		{
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				 WebView.HitTestResult hr = ((WebView)v).getHitTestResult();

				 Log.i("webinfo", "getExtra = "+ hr.getExtra() + "\t\t Type=" + hr.getType());
	                return false;
			}
        });
		
		
		
//		webView.loadUrl("http://www.hdpfans.com/thread-435-1-1.html");
		webView.loadUrl(abird_url);
	}

	class JSinterface
	{
		private InfoSearcher instance;

		public void getHTML(String html)
		{
//			System.out.println(html);
			instance = InfoSearcher.getInstance(html);
			HashMap<String, String> infoMap = instance.useRegularExpressions();
			
			InfoProvider provider = InfoProvider.getInstance(getApplicationContext());
			ContentValues values = new ContentValues();
			values.put(InfoProvider.COL_AUTHOR, infoMap.get("author"));
			provider.insert(InfoProvider.mUri, values);
			provider.insert(InfoProvider.mUri, values);
			Log.d("webinfo", Arrays.toString(provider.selectAll().toArray()));
			provider.delete(Uri.withAppendedPath(InfoProvider.mUri, "3"),  null, null);
			Log.d("webinfo", Arrays.toString(provider.selectAll().toArray()));
		}
		
	}
	
	
}

FireBug和真实的HTML源代码不一样啊有木有!!!坑爹啊有木有!!!坑的我怎样都提取不到标

签和内容啊有木有!!!

相关推荐