web视图shouldinterceptrequest例子 [英] webview shouldinterceptrequest example

查看:544
本文介绍了web视图shouldinterceptrequest例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何做的 shouldinterceptrequest 的工作。我不知道如何创建和处理这种方法来读取和替换的CSS链接。谢谢!

I need to know how does the shouldinterceptrequest work. I don't know how to create and handle this method to read and replace the CSS link. Thank you!

推荐答案

嗯,简单的答案是,它的工作原理非常类似于 shouldOverrideUrlLoading(web视图查看,字符串URL) ,如图中 web视图教程

Well, the short answer is that it works quite similar to shouldOverrideUrlLoading(WebView view, String url), as illustrated in the WebView tutorial.

为帮助您开始,请参见下面的code。您只需重写 shouldInterceptRequest(web视图查看,字符串URL)的WebViewClient的方法。很明显,你不必做内联,但为了紧凑这就是我所做的:

To get you started, see the code below. You simply override the shouldInterceptRequest(WebView view, String url) method of your WebViewClient. Obviously you don't have to do that inline, but for the sake of compactness that's what I did:

    WebView webview = (WebView) findViewById(R.id.webview);
    webview.setWebViewClient(new WebViewClient() {
        @Override
        public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
            if (url.contains(".css")) {
                return getCssWebResourceResponseFromAsset();
            } else {
                return super.shouldInterceptRequest(view, url);
            }
        }

        /**
         * Return WebResourceResponse with CSS markup from a String. 
         */
        @SuppressWarnings("deprecation")
        private WebResourceResponse getCssWebResourceResponseFromString() {
            return getUtf8EncodedCssWebResourceResponse(new StringBufferInputStream("body { background-color: #F781F3; }"));
        }

        /**
         * Return WebResourceResponse with CSS markup from an asset (e.g. "assets/style.css"). 
         */
        private WebResourceResponse getCssWebResourceResponseFromAsset() {
            try {
                return getUtf8EncodedCssWebResourceResponse(getAssets().open("style.css"));
            } catch (IOException e) {
                return null;
            }
        }

        /**
         * Return WebResourceResponse with CSS markup from a raw resource (e.g. "raw/style.css"). 
         */
        private WebResourceResponse getCssWebResourceResponseFromRawResource() {
            return getUtf8EncodedCssWebResourceResponse(getResources().openRawResource(R.raw.style));
        }

        private WebResourceResponse getUtf8EncodedCssWebResourceResponse(InputStream data) {
            return new WebResourceResponse("text/css", "UTF-8", data);
        }

    });

    webview.loadUrl("http://stackoverflow.com");

赶上CSS文件的加载,回到自己的<一href="http://developer.android.com/reference/android/webkit/WebResourceResponse.html"><$c$c>WebResourceResponse包含要代替中加载数据。

Catch the loading of the css file and return your own WebResourceResponse containing the data you want to load in stead.

请注意,这种方法需要的 API级别11

Do note that this method requires API level 11.

如果你想要做类似的针对Android 2.X的东西,你可能想尝试使用前面提到的 shouldOverrideUrlLoading(web视图查看,字符串URL),以避免加载页面,手动取水的时候,更换参考用自己的CSS文件,最后调用 loadData(字符串数据,字符串MIMETYPE,字符串编码)(或 loadDataWithBaseURL(字符串的baseUrl,字符串数据,字符串MIMETYPE,编码字符串,字符串historyUrl))的web视图,传递操纵HTML内容为一个字符串。

If you want to do something similar for Android 2.x, you might want to try using the earlier mentioned shouldOverrideUrlLoading(WebView view, String url) to avoid loading the page, fetch it manually, replace the reference to the css file with your own, and finally call loadData(String data, String mimeType, String encoding) (or loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)) on the WebView, passing in the manipulated html content as a string.

这篇关于web视图shouldinterceptrequest例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆