android webclient使用

public class MyWebViewClient extends Activity
{
   
    private WebView webView;
   
    private final String mimeType = "text/html";
   
    private final String encoding = "utf-8";
   
    @Override
    protected void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setContentView(R.layout.viewclient);
        webView = (WebView) findViewById(R.id.webview);
       
        webView.getSettings().setJavaScriptEnabled(true);
       
        /*
         * 取值
         */
        Bundle myBundle = getIntent().getExtras();
        String url =  myBundle.getString("url");
       
        if (CommonUtil.isNotEmpty(url))
        {
            if (url.startsWith("http"))
            {
                openWebRequest(url);
            }
            else if (url.startsWith("file"))
            {
                openLocalFile(url);
            }
            else
            {
                localHtmlAndImage(url);
            }
        }
       
    }
   
    /**
     * 打开网页  / 直接网络图片显示 
     *
     * @param String url(URL路径)
     * @return void
     * @throws
     * @since TianTian
     */
    private void openWebRequest(String url)
    {
       
        try
        {
            webView.loadUrl(url);
           
        }
        catch (Exception ex)
        {
           
            ex.printStackTrace();
           
        }
       
    }
   
   
    /**
     * 中文显示(空格的处理)
     *
     * @param String data(数据)
     * @return void
     * @throws
     * @since TianTian
     */
    @SuppressWarnings("unused")
    private void localHtmlBlankSpace(String data)
    {
        try
        {
           
            if (CommonUtil.isNotEmpty(data))
            {
                webView.loadData(URLEncoder.encode(data, encoding),mimeType,encoding);
                webView.loadData(URLEncoder.encode(data, encoding).replaceAll("\\+", " "),mimeType,encoding);
            }  
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
   
    /**
     * 显示本地网页文件    / 显示本地图片文件  
     *
     * @param String data(数据)
     * @return void
     * @throws
     * @since TianTian
     */
    private void openLocalFile(String url)
    {
        try
        {  
            // 本地文件处理(如果文件名中有空格需要用+来替代) 
            webView.loadUrl(url);
        }
        catch (Exception ex)
        {  
            ex.printStackTrace();
        }
    }
   
    /**
     * 显示本地图片和文字混合的Html内容 
     *
     * @param String data(数据)
     * @return void
     * @throws
     * @since TianTian
     */
    private void localHtmlAndImage(String data)
    {
        try
        {
            if (CommonUtil.isNotEmpty(data))
            {  webView.loadDataWithBaseURL("about:blank",data,mimeType,encoding,"");
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

xml:

android:layout_width="fill_parent" android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="fill_parent" android:id="@+id/webview" />

你可能感兴趣的:(Android)