webview应该拦截请求的例子

我需要知道shouldinterceptrequest是如何工作的。 我不知道如何创build和处理这个方法来读取和replaceCSS链接。 谢谢!

Solutions Collecting From Web of "webview应该拦截请求的例子"

那么,简单的答案就是它的工作原理与WebView教程中所示的shouldOverrideUrlLoading(WebView view, String url)非常相似。

为了让你开始,请看下面的代码。 您只需重写WebViewClient的shouldInterceptRequest(WebView view, String url)方法即可。 显然你不需要这样做,但为了紧凑,这就是我所做的:

  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 (eg "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 (eg "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包含您要加载的数据。

请注意,此方法需要API级别11

如果你想为Android 2.x做类似的事情,你可能想尝试使用前面提到的shouldOverrideUrlLoading(WebView view, String url)来避免加载页面,手动获取它,用你自己的replacecss文件的引用,最后在WebView中调用loadData(String data, String mimeType, String encoding) (或loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) ),并以loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)传递被操作的html内容。

之前:

之前

后:

后

这可能对你也很有趣。 它从特定的文件夹中读取文件,如果请求以这个文件名传入,它将使用资产文件夹中的文件,而不是来自web的文件。

  //get list of files of specific asset folder private ArrayList listAssetFiles(String path) { List myArrayList = new ArrayList(); String [] list; try { list = getAssets().list(path); for(String f1 : list){ myArrayList.add(f1); } } catch (IOException e) { e.printStackTrace(); } return (ArrayList) myArrayList; } //get mime type by url public String getMimeType(String url) { String type = null; String extension = MimeTypeMap.getFileExtensionFromUrl(url); if (extension != null) { if (extension.equals("js")) { return "text/javascript"; } else if (extension.equals("woff")) { return "application/font-woff"; } else if (extension.equals("woff2")) { return "application/font-woff2"; } else if (extension.equals("ttf")) { return "application/x-font-ttf"; } else if (extension.equals("eot")) { return "application/vnd.ms-fontobject"; } else if (extension.equals("svg")) { return "image/svg+xml"; } type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); } return type; } //return webresourceresponse public WebResourceResponse loadFilesFromAssetFolder (String folder, String url) { List myArrayList = listAssetFiles(folder); for (Object str : myArrayList) { if (url.contains((CharSequence) str)) { try { Log.i(TAG2, "File:" + str); Log.i(TAG2, "MIME:" + getMimeType(url)); return new WebResourceResponse(getMimeType(url), "UTF-8", getAssets().open(String.valueOf(folder+"/" + str))); } catch (IOException e) { e.printStackTrace(); } } } return null; } //@TargetApi(Build.VERSION_CODES.LOLLIPOP) @SuppressLint("NewApi") @Override public WebResourceResponse shouldInterceptRequest(final WebView view, String url) { //Log.i(TAG2, "SHOULD OVERRIDE INIT"); //String url = webResourceRequest.getUrl().toString(); String extension = MimeTypeMap.getFileExtensionFromUrl(url); //I have some folders for files with the same extension if (extension.equals("css") || extension.equals("js") || extension.equals("img")) { return loadFilesFromAssetFolder(extension, url); } //more possible extensions for font folder if (extension.equals("woff") || extension.equals("woff2") || extension.equals("ttf") || extension.equals("svg") || extension.equals("eot")) { return loadFilesFromAssetFolder("font", url); } return null; }