webview shouldinterceptrequest 示例 [英] webview shouldinterceptrequest example

查看:21
本文介绍了webview 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(WebView view, String url) 非常相似,如 <一个 href="http://developer.android.com/resources/tutorials/views/hello-webview.html" rel="nofollow noreferrer">WebView 教程.

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

要开始使用,请参阅下面的代码.您只需覆盖 WebViewClient 的 shouldInterceptRequest(WebView view, String url) 方法.显然,您不必执行内联操作,但为了紧凑起见,我就是这样做的:

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 ByteArrayInputStream("body { background-color: #F781F3; }".getBytes()));
        }

        /**
         * 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文件的加载并返回自己的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(WebView view, String url) 以避免加载页面,手动获取它,用自己的替换css文件的引用,最后调用loadData(String data, String mimeType, String encoding)(或loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding), String historyUrl)) 在WebView上,将被操作的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.

之前:

之后:

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

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