Webview第一次没有加载

时间:2015-01-27 03:18:55

标签: android webview https

我正在努力解决一个奇怪的问题。我试图加载https网页,但第一次没有加载webview。等待60秒后,我必须再次单击我的按钮才能加载我的页面。我的设备是带有Lollipop的Nexus 4,但这个问题也出现在Android 4.4和4.1的设备上。该网址只有少量javascript文件和css文件。

记录:

 I/WebViewFactory﹕ Loading com.google.android.webview version 37 (1602158-arm) (code 111201)
 I/LibraryLoader﹕ Loading: webviewchromium
 I/LibraryLoader﹕ Time to load native libraries: 3 ms (timestamps 5331-5334)
 I/LibraryLoader﹕ Expected native library version number "",actual native library version number ""
 I/LibraryLoader﹕ Expected native library version number "",actual native library version number ""
 I/chromium﹕ [INFO:library_loader_hooks.cc(106)] Chromium logging enabled: level = 0, default verbosity = 0
 I/BrowserStartupController﹕ Initializing chromium process, renderers=0
 W/art﹕ Attempt to remove local handle scope entry from IRT, ignoring
 W/chromium﹕ [WARNING:resource_bundle.cc(315)] locale_file_path.empty()
 I/chromium﹕ [INFO:aw_browser_main_parts.cc(63)] Load from apk succesful, fd=72 off=159196 len=3264
 I/chromium﹕ [INFO:aw_browser_main_parts.cc(78)] Loading webviewchromium.pak from, fd:73 off:229484 len:643667
 W/AudioManagerAndroid﹕ Requires BLUETOOTH permission
 W/chromium﹕ [WARNING:proxy_service.cc(901)] PAC support disabled because there is no system implementation
 W/chromium﹕ [WARNING:data_reduction_proxy_settings.cc(403)] SPDY proxy OFF at startup
 W/art﹕ Attempt to remove local handle scope entry from IRT, ignoring
 W/AwContents﹕ onDetachedFromWindow called when already detached. Ignoring
 I/chromium﹕ [INFO:SkUtilsArm.cpp(179)] Device supports ARM NEON instructions!

我的代码:

final WebView wv = (WebView) alert.findViewById(R.id.modal_wv);

    wv.getSettings().setAppCacheEnabled(true);
    wv.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    wv.getSettings().setAppCachePath("/data/data/" + getPackageName() + "/cache");
    wv.getSettings().setAllowFileAccess(true);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.loadUrl(connectionResponse.getUrl());
/*
//Same behavior ...
    wv.post(new Runnable() {
        @Override
        public void run() {
            wv.loadUrl(connectionResponse.getUrl());
        }
    });
*/

我设置了一个新的WebClient()来覆盖以下方法:shouldOverrideUrlLoading,onLoadResource,onPageFinished。为了测试目的,我删除了这个自定义WebClient,但它仍然没有在第一时间加载。

由于

4 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。我花了3天多的时间找到了另一个WebView对象调用pauseTimers()来保存一些CPU性能的原因,这实际上是"暂停所有WebView的所有布局,解析和JavaScript计时器。"

答案 1 :(得分:0)

由于您在webview中加载 https 网址,因此会导致SSL认证问题 因此,您必须覆盖另一种处理认证问题的方法并处理它 这是代码如何解决它

myWebView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            Toast.makeText(MainActivity.this, description,
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onReceivedSslError(WebView view,
                SslErrorHandler handler, SslError error) {
            // TODO Auto-generated method stub
            // this method will proceed your url however if certification issues are there or not
            handler.proceed();
        }

    });
myWebView.loadUrl(url);

编辑:如果您要为您的网页视图启用javascript界面 执行以下步骤

private JavascriptInterface jsInterface;


engine.getSettings().setJavaScriptEnabled(true);
        jsInterface = new JavascriptInterface();
        try {
            ComponentName comp = new ComponentName(this, Dashboard.class);
            PackageInfo pinfo = getPackageManager().getPackageInfo(comp.getPackageName(), 0);
            jsInterface.versionCode = pinfo.versionCode;
            } catch (android.content.pm.PackageManager.NameNotFoundException e) {
            }
            engine.addJavascriptInterface(jsInterface, "androidlearnscripture");
                            engine.requestFocus(View.FOCUS_DOWN);
        }

javascript代码将在此处

    public void onPageStarted(WebView view, String url,Bitmap favicon) {
            jsInterface.enablePreferencesMenu = false;
            jsInterface.modalIsVisible = false;
            jsInterface.urlForSharing = null;
            bLoadingFinished = false;
}

答案 2 :(得分:0)

我通过在完成加载后从功能onPageFinished(WebView view, String url)向布局添加webview解决了同样的问题。

答案 3 :(得分:-1)

您是否在androidmanifest.xml中声明了互联网权限?

<manifest...>
.. .
<uses-permission android:name="android.permission.INTERNET" />
..
</manifest>

也尝试设置一个webview客户端。

          //default browser
          wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                L.log(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        //chrome browser
        wv.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                progressBar.setProgress(progress);
                if (progress == 100) {
                    progressBar.setVisibility(View.GONE);
                }
            }

        });