javascript - 关于android , webview 重写shouldInterceptRequest方法,但是导致js代码失败

标签 javascript android webview

谢谢:

webview加载一个url(www.simple.com/step1.html),响应内容

<html>
<body>
    <script>
        location.href = "/step2.html"
    </script>
</body>
</html>

js代码不起作用。正常情况下,最终页面将是step2。但仍处于步骤1 当我重写 shouldInterceptRequest 时:

public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
             try {
                URL oUrl = new URL(request.getUrl().toString());

                final HttpURLConnection conn = (HttpURLConnection)oUrl.openConnection();
                CookieManager cookieManager = CookieManager.getInstance();
                String cookie = cookieManager.getCookie(oUrl.getHost());
                if(!TextUtils.isEmpty(cookie))
                    conn.setRequestProperty("Cookie", cookie);
                conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
                conn.setRequestProperty("Connection", "keep-alive");
                conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
                conn.setRequestProperty("Upgrade-Insecure-Requests", "1");
                conn.setRequestProperty("Accept-Language", Locale.getDefault().toString()+";q=0.8");
                conn.setRequestMethod(request.getMethod());
                conn.setInstanceFollowRedirects(true);
                conn.connect();
                BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()){
                    @Override
                    public void close() throws IOException {
                        super.close();
                        if (conn != null) {
                            conn.disconnect();
                        }
                    }

                };

                return new WebResourceResponse(conn.getContentType(), conn.getContentEncoding(), bis);
            } catch (Throwable e) {
                e.printStackTrace();
            }
            return super.shouldInterceptRequest(view, request);
        }

最佳答案

您是否在 WebView 中启用了 JavaScript?代码是这样的:

myWebView.getSettings().setJavaScriptEnabled(true);

关于javascript - 关于android , webview 重写shouldInterceptRequest方法,但是导致js代码失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47388139/

相关文章:

javascript - SectionList 中的选项卡 React Native

javascript - 为什么这个自定义 jQuery 提示每第 n 次使用它就重复它的回调操作 n 次?

java - 单击时出现 NullPointerException,即使按钮变量设置为 "field variable"

android - 什么是 jni/libs 文件夹?

javascript - 在 WebView 中加载 YouTube 视频时全屏选项不可用

javascript - 如何检测 WebView Android 中的按钮点击?

android - 链接不在 webview 中打开?

javascript - 如何显示12小时制并显示实际时间和存储时间之间的时差

javascript - 如何在同一个html页面中制作后退按钮

java - Android Studio问题|等待进程出现在设备上超时