在我的Android应用中,我有一个带有WebView的屏幕。
此屏幕的逻辑:用户单击搜索按钮。 Webview打开,URL为“ https://xn--90adear.xn--p1ai/check/auto”。 JS脚本查找appVehicleCheck.reCaptchaToken变量,然后app向我的API发送另一个请求。
这在除Android 4.4(API 19)之外的所有Android上均能完美运行。我收到此错误:I /铬:[INFO:CONSOLE(1)]“未捕获的TypeError:无法读取未定义的属性'length'”,来源:https://xn--90adear.xn--p1ai/assets/js/ext.js?201808012357(1)
我的代码:
private void reloadWebView() {
dismissProgressDialog();
progressView.setVisibility(View.VISIBLE);
captchaWebView.loadUrl(URL_GET_GIBDD);
captchaWebView.getSettings().setJavaScriptEnabled(true);
captchaWebView.getSettings().setDomStorageEnabled(true);
captchaWebView.getSettings().setLoadsImagesAutomatically(false);
captchaWebView.addJavascriptInterface(new JavaScriptInterface(captchaWebView), "AutoinsInterface");
captchaWebView.setWebViewClient(new AutoinsWebViewClient());
}
public class AutoinsWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
public void onReceivedError(android.webkit.WebView view, WebResourceRequest request, WebResourceError error) {
view.stopLoading();
captchaWebView.setVisibility(View.GONE);
final RelativeLayout samplesMain = (RelativeLayout) findViewById(R.id.root_layout);
mSnackbar = Snackbar.make(samplesMain, FullReportActivity.this.getResources().getString(R.string.insurance_error), Snackbar.LENGTH_INDEFINITE)
.setAction("Action", null)
.setDuration(2000);
mSnackbar.show();
progressView.setVisibility(View.GONE);
}
public void onReceivedSslError(android.webkit.WebView view, SslErrorHandler handler, SslError er) {
SslErrorHandler sslErrorHandler = handler;
if (er.getUrl().equals(URL_GET_GIBDD)) {
sslErrorHandler.proceed();
} else {
sslErrorHandler.cancel();
}
progressView.setVisibility(View.GONE);
}
public void onPageFinished(android.webkit.WebView view, String url) {
super.onPageFinished(view, url);
if (url.equals(URL_GET_GIBDD)) {
if (url.equals(URL_GET_GIBDD)) {
view.loadUrl("javascript:$('.ln-header').hide()");
view.loadUrl("javascript:$('.ln-footer').hide()");
view.loadUrl("javascript:$('.bn-federal-site').hide()");
view.loadUrl("javascript:$('.bn-top-menu').hide()");
view.loadUrl("javascript:$('.ln-content-right').hide()");
view.loadUrl("javascript:$('.widget-mistake').hide()");
view.loadUrl("javascript:$('.b-mobile-section').hide()");
view.loadUrl("javascript:$('h1,h2').hide()");
view.loadUrl("javascript:$('.ln-page').hide()");
view.loadUrl("javascript:getCookie = function(name) {\n" +
" var r = document.cookie.match(\"\\\\b\" + name + \"=([^;]*)\\\\b\");\n" +
" return r ? r[1] : null;\n" +
" };\n" +
"\n" +
" var session = getCookie('session');" );
view.loadUrl("javascript:var int; int = setInterval(function(){if(appVehicleCheck.reCaptchaToken.length > 0){ window.AutoinsInterface.make(appVehicleCheck.reCaptchaToken, session); clearInterval(int); } },1000);");
}
}
}
}
public class JavaScriptInterface {
private WebView webView;
public JavaScriptInterface(WebView webView) {
this.webView = webView;
}
@JavascriptInterface
public void make(String captcha_answer, String sessionId) throws Exception {
if (captcha_answer != null && captcha_answer.length() > 0) {
/* url, captcha, checktype, vin*/
ArrayList<String> passing = new ArrayList<String>();
passing.add(vinNumber);
passing.add(sessionId);
passing.add(captcha_answer);
progressView.setVisibility(View.VISIBLE);
sendGibddRequest = new SendGibddRequest();
try {
sendGibddRequest.execute(passing);
} catch (Exception e) {
e.printStackTrace();
}
} else {
captchaWebView.setVisibility(View.GONE);
progressView.setVisibility(View.GONE);
}
}
}
我不明白为什么仅在Android 4.4上会出现此问题?
我已经尝试使用view.evaluateJavascript()代替view.loadUrl()。这没有帮助。
我在某些设备上对onPageFinished的调用过多也存在问题。我自己没有重现此问题,但可以在Google Analytics(分析)中看到。也许有人可以帮我这个忙。