我正在使用WebView来显示信息的项目中,我应该使用shouldInterceptRequest添加标头。一切正常,但是在更新chrome 76之后,它显示空白页,仅应调用InterceptRequest。
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setLoadWithOverviewMode(false);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.clearCache(true);
webView.getSettings().setAppCacheEnabled(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
webView.setWebViewClient(new WebViewClient() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Nullable
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
Log.d("TAG", "shouldInterceptRequest: 2");
if (!checkFirstLoad) {
try {
checkFirstLoad = true;
Request req;
OkHttpClient httpClient = new OkHttpClient();
req = new Request.Builder()
.url(PA_URL.trim())
.addHeader("Pa-Token", token)
.addHeader("Pa-Redirect", url)
.addHeader("Pa-App-Token", randomString())
.addHeader("Pa-App-Encrypt", md5(deviceId+oldToken)).build();
Response response = httpClient.newCall(req).execute();
return new WebResourceResponse(MimeTypeMap.getFileExtensionFromUrl(request.getUrl().toString()),
response.header("content-encoding", "utf-8"),
response.body().byteStream());
} catch (IOException e) {
e.printStackTrace();
return null;
}
} else {
return null;
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
Log.d("TAG", "shouldOverrideUrlLoading: "+url);
return true;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
Log.d("TAG", "shouldOverrideUrlLoading: "+request.getUrl().toString());
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
Log.d("TAG", "onPageStarted: "+url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
Log.d("TAG", "onPageFinished: "+url);
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
handler.proceed();
Log.d("TAG", "onReceivedError: "+error);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
progressBar.setVisibility(View.GONE);
Log.d("TAG", "onReceivedError: "+error);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
progressBar.setVisibility(View.GONE);
Log.d("TAG", "onReceivedError: "+ description);
}
});
webView.loadUrl(url);
答案 0 :(得分:1)
它不适用于 POST 请求,因为WebResourceRequest
不提供 POST 数据。 This是一个类,在完整的浏览器中支持多种 HTTP 请求。
答案 1 :(得分:0)
我也面临这个问题。发生这种情况的原因是您已启用dom存储。.从chrome清除缓存并清除应用数据,卸载并再次运行。.在我看来,它是一种魅力。