Android5.0+ webView+vue,使用iframe加载云端文档URL时出现了报错,加载网页失败
由于响应头添加X-Frame-Option为’SAMEORIGIN’,导致了问题的产生
解决办法:
1、服务器修改配置
2、我们可以在webView中忽略X-Frame-Option,示例代码 如下
首先,在app/build.gradle中引入依赖

api 'org.jsoup:jsoup:1.12.1'

其次,在webViewClient中配置

webView.setWebViewClient(new WebViewClient(){
	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    	WebResourceResponse content = getsPageContent(request.getUrl().toString());
        	if (content != null) {
                return content;
            } else {
                return super.shouldInterceptRequest(view, request);
            }
    }

    @Nullable
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
           WebResourceResponse content = getsPageContent(url);
           if (content != null) {
           		return content;
           } else {
             	return super.shouldInterceptRequest(view, url);
           }
   }
});

private WebResourceResponse getsPageContent(String url){
     	if (( url.contains("http")) // guess is better to create for ex map to check all extensions
                && !(( url.contains(".js"))
                ||( url.contains(".css"))
                ||( url.contains(".tiff"))
                ||( url.contains(".jpg"))
                ||( url.contains(".png"))
                ||( url.contains(".gif")))){
            try {
                return new WebResourceResponse(
                        "text/html",
                        "UTF-8",
                        new ByteArrayInputStream(
                                Jsoup.connect(url)
                                        .get()
                                        .toString()
                                        .getBytes()));
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       return null;
}
Logo

前往低代码交流专区

更多推荐