我试图从使用JavaScript的网站中检索html。我的方法如下:
有趣的是,这段代码有效,但不一致。有一次它会按预期工作,下次什么都不会发生。我收到各种奇怪的logcat消息,其中一个我怀疑可能是一个不好的信号:W/art: Attempt to remove non-JNI local reference, dumping thread
。
今天出现了一个想法:有时我的WebView会被杀死,因为运行时环境会进行优化(而我的WebView没有链接到它的XML View对象)?
所以,我决定尝试在我的XML中创建一个WebView,并使用相同的代码而不是在代码中创建一个。现在,代码一直运行,每次运行时,我都不再收到那些线程转储日志消息。只要我看到页面完成加载,代码就会被执行。
请注意,我注意到我引用的这个特定网站需要相对较长的时间才能加载(只需几秒钟),并且onPageFinished也会被多次调用,除非它是一个未经处理的JS提供html最后一次电话。
所以,我的问题是:在使用无头版本(或任何其他解决方法)时,有没有办法阻止ART转储我的线程?我想保持在XML中定义的WebView,并将其可见性设置为false,但它似乎是一个资源密集型解决方案。
我正在使用Android模拟器(虽然我也使用物理设备获得了相同的结果),目标API为24,最低API 15。
相关代码:
private void fetchHTML(View view, String url) {
class JSInterface{
@JavascriptInterface
public void processHTML(String html){
Log.d(LOG_TAG, html); // This bit of code sometimes gets executed, and sometimes does not
}
}
//final WebView webView = (WebView) view.findViewById(R.id.webview);
final WebView webView = new WebView(context);
WebSettings settings = webView.getSettings();
settings.setDomStorageEnabled(true);
webView.addJavascriptInterface(new JSInterface(), "Android");
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(webView.getProgress() == 100){
webView.loadUrl("javascript:window.Android.processHTML("
+ "'<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
}
});
settings.setJavaScriptEnabled(true);
webView.loadUrl(url);
}
未正确执行时的相关logcat:
07-15 17:16:27.616 17779-17779/com.michaelsvit.kolnoa W/System: ClassLoader referenced unknown path: /data/app/com.michaelsvit.kolnoa-2/lib/x86
07-15 17:16:28.214 17779-17779/com.michaelsvit.kolnoa W/System: ClassLoader referenced unknown path: /data/app/com.michaelsvit.kolnoa-2/lib/x86
07-15 17:16:28.267 17779-17779/com.michaelsvit.kolnoa W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-15 17:16:28.448 17779-17779/com.michaelsvit.kolnoa I/WebViewFactory: Loading com.android.webview version 44.0.2403.119 (code 246011910)
07-15 17:16:28.456 17779-17779/com.michaelsvit.kolnoa W/System: ClassLoader referenced unknown path: /system/app/webview/lib/x86
07-15 17:16:28.465 17779-17779/com.michaelsvit.kolnoa I/LibraryLoader: Time to load native libraries: 3 ms (timestamps 8656-8659)
07-15 17:16:28.465 17779-17779/com.michaelsvit.kolnoa I/LibraryLoader: Expected native library version number "",actual native library version number ""
07-15 17:16:28.470 17779-17779/com.michaelsvit.kolnoa V/WebViewChromiumFactoryProvider: Binding Chromium to main looper Looper (main, tid 1) {c4bffce}
07-15 17:16:28.470 17779-17779/com.michaelsvit.kolnoa I/LibraryLoader: Expected native library version number "",actual native library version number ""
07-15 17:16:28.470 17779-17779/com.michaelsvit.kolnoa I/chromium: [INFO:library_loader_hooks.cc(120)] Chromium logging enabled: level = 0, default verbosity = 0
07-15 17:16:28.476 17779-17779/com.michaelsvit.kolnoa I/BrowserStartupController: Initializing chromium process, singleProcess=true
07-15 17:16:28.479 17779-17779/com.michaelsvit.kolnoa W/art: Attempt to remove non-JNI local reference, dumping thread
07-15 17:16:28.479 17779-17779/com.michaelsvit.kolnoa E/SysUtils: ApplicationContext is null in ApplicationStatus
07-15 17:16:28.484 17779-17779/com.michaelsvit.kolnoa W/chromium: [WARNING:resource_bundle.cc(285)] locale_file_path.empty()
07-15 17:16:28.487 17779-17779/com.michaelsvit.kolnoa E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
07-15 17:16:28.487 17779-17779/com.michaelsvit.kolnoa E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY)
[ 07-15 17:16:28.488 17779:17779 D/ ]
HostConnection::get() New Host Connection established 0xac3edff0, tid 17779
07-15 17:16:28.525 17779-17779/com.michaelsvit.kolnoa E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008d57
07-15 17:16:28.552 17779-17779/com.michaelsvit.kolnoa E/DataReductionProxySettingListener: No DRP key due to exception:java.lang.ClassNotFoundException: com.android.webview.chromium.Drp
07-15 17:16:28.568 17779-17831/com.michaelsvit.kolnoa W/AudioManagerAndroid: Requires BLUETOOTH permission
07-15 17:16:28.578 17779-17779/com.michaelsvit.kolnoa W/art: Attempt to remove non-JNI local reference, dumping thread
07-15 17:16:28.582 17779-17779/com.michaelsvit.kolnoa W/AwContents: onDetachedFromWindow called when already detached. Ignoring
07-15 17:16:28.584 17779-17779/com.michaelsvit.kolnoa W/art: Attempt to remove non-JNI local reference, dumping thread
07-15 17:16:28.585 17779-17779/com.michaelsvit.kolnoa W/AwContents: onDetachedFromWindow called when already detached. Ignoring
07-15 17:16:28.587 17779-17779/com.michaelsvit.kolnoa W/art: Attempt to remove non-JNI local reference, dumping thread
07-15 17:16:28.587 17779-17779/com.michaelsvit.kolnoa W/art: Attempt to remove non-JNI local reference, dumping thread
07-15 17:16:28.620 17779-17841/com.michaelsvit.kolnoa D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-15 17:16:28.639 17779-17829/com.michaelsvit.kolnoa W/chromium: [WARNING:data_reduction_proxy_config.cc(423)] SPDY proxy OFF at startup
07-15 17:16:28.740 17779-17841/com.michaelsvit.kolnoa I/OpenGLRenderer: Initialized EGL, version 1.4
[ 07-15 17:16:28.741 17779:17841 D/ ]
HostConnection::get() New Host Connection established 0xa0a5f8d0, tid 17841
07-15 17:16:28.782 17779-17841/com.michaelsvit.kolnoa W/EGL_emulation: eglSurfaceAttrib not implemented
07-15 17:16:28.782 17779-17841/com.michaelsvit.kolnoa W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaa3d9180, error=EGL_SUCCESS
[ 07-15 17:16:28.815 17779:17850 D/ ]
HostConnection::get() New Host Connection established 0xa0c82c80, tid 17850
07-15 17:16:29.166 17779-17779/com.michaelsvit.kolnoa W/BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 17779
07-15 17:16:29.279 17779-17779/com.michaelsvit.kolnoa D/MovieGridFragment:
07-15 17:16:29.768 17779-17779/com.michaelsvit.kolnoa W/BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 17779
07-15 17:16:30.894 17779-17779/com.michaelsvit.kolnoa I/chromium: [INFO:CONSOLE(2)] "Uncaught TypeError: Cannot read property 'length' of undefined", source: (2)
07-15 17:16:31.196 17779-17829/com.michaelsvit.kolnoa W/chromium: [WARNING:spdy_session.cc(2527)] Received WINDOW_UPDATE for invalid stream 1
答案 0 :(得分:0)
按照我提出的想法,我保留了XML定义的WebView(可见性设置为&#34;消失了#34;)并使用它而不是在运行时创建它。从那时起我就没有任何问题了。