谢谢:
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/