Android - WebViewClient 加载页面极其缓慢

发布于 2024-10-11 05:30:55 字数 414 浏览 1 评论 0原文

作为一名开发人员,在构建在 3G 连接的 Android webviewclient 上运行的应用程序(带有网页)时,我有过非常糟糕的经历。我的一些用户表示某些页面需要 10 分钟才能加载。 (我不知道他们是否夸大其词,因为他们感到沮丧。)就我个人而言,我使用运行 Android 2.2 的 HTC Desire,并用我自己的手机测试了我的应用程序。事实上,即使对于使用本地存储的网页来发出 AJAX 请求的应用程序,它在 3G 上也非常慢。大多数时候,它只会超时并显示空白页面。当它在 WIFI 上时,它工作正常并且速度非常快。

我听很多人说 Android 2.2 上的本机浏览器速度非常快。但对我来说,我必须说本机浏览器应用程序在 3G 上也非常慢。有时,Google 移动主页甚至无法完成加载。我在新加坡,所以大多数地方 3G 覆盖应该都很好。

是否是设备硬件导致了问题?

As a developer, I have had pretty bad experiences when it comes to building apps (with web pages) that run on the Android webviewclient on 3G connection. A few of my users are saying that some pages take up to 10 minutes to load. (I don't know if they have exaggerated because they are frustrated.) Personally, I use HTC Desire running Android 2.2 and have tested my apps with my own phone. Indeed, even for apps that use locally stored web pages to make AJAX requests, it is extremely slow on 3G. Most of the time, it will just timeout and show up a blank page. When it's on WIFI, it works fine and is real fast.

I have heard many say that the native browser on Android 2.2 is extremely fast. For me, I must however say that the native browser app is also very slow on 3G. Sometimes, the Google mobile home page does not even finish loading. I'm in Singapore and so 3G coverage should be fine in most places.

Is it the device hardware that is causing the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山田美奈子 2024-10-18 05:30:55

我不知道与 WebView 加载时间特别相关的设备特定性能问题。但是,您可能想要添加请求超时和网络连接更改侦听器。有关其他信息,您可以对请求进行计时并报告有关缓慢请求的设备和连接信息。

handler = new Handler();
WebView webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
    final long WEBVIEW_REQUEST_TIMEOUT = ...;
    final long WEBVIEW_SLOW_REQUEST_THRESHOLD = ...;
    Runnable onRequestTimeout = null;
    @Override
    public void onPageStarted(WebView view, final String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon); // current impl is no op but be fwd compatible
        saveRequestStartTime();
        onRequestTimeout = new Runnable() {
            @Override
            public void run() {
                handleRequestComplete(url, WebViewClient.ERROR_TIMEOUT);
            }
        };
        handler.postDelayed(onRequestTimeout, WEBVIEW_REQUEST_TIMEOUT);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url); // current impl is no op but be fwd compatible
        handleRequestComplete(url, 0);   // Return 0 onSuccess. All WebViewClient defined errors < 0. 
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl); // current impl is no op but be fwd compatible
        handleRequestComplete(failingUrl, errorCode);
    }

    private void handleRequestComplete(String url, int errorCode) {
        if (onRequestTimeout != null) {
            handler.removeCallbacks(onRequestTimeout);
        }
        onRequestTimeout = null;
        final long elapsedTime = getRequestElapsedTime();
        if (elapsedTime > WEBVIEW_SLOW_REQUEST_THRESHOLD) {
            reportSlowRequest(elapsedTime, url, errorCode);
        }
        clearRequestTime();
    }

    private void saveRequestStartTime() {...}
    private long getRequestElapsedTime() {...}
    private void clearRequestTime() {...}
    private void reportSlowRequest(long elapsedTime, String url, int errorCode) {...}
});

I am not aware of device specific performance issues specifically related to WebView load times. However, you may want to add both a request timeout and a network connectivity change listener. For additional information you can time requests and report back device and connectivity information on slow requests.

handler = new Handler();
WebView webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
    final long WEBVIEW_REQUEST_TIMEOUT = ...;
    final long WEBVIEW_SLOW_REQUEST_THRESHOLD = ...;
    Runnable onRequestTimeout = null;
    @Override
    public void onPageStarted(WebView view, final String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon); // current impl is no op but be fwd compatible
        saveRequestStartTime();
        onRequestTimeout = new Runnable() {
            @Override
            public void run() {
                handleRequestComplete(url, WebViewClient.ERROR_TIMEOUT);
            }
        };
        handler.postDelayed(onRequestTimeout, WEBVIEW_REQUEST_TIMEOUT);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url); // current impl is no op but be fwd compatible
        handleRequestComplete(url, 0);   // Return 0 onSuccess. All WebViewClient defined errors < 0. 
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl); // current impl is no op but be fwd compatible
        handleRequestComplete(failingUrl, errorCode);
    }

    private void handleRequestComplete(String url, int errorCode) {
        if (onRequestTimeout != null) {
            handler.removeCallbacks(onRequestTimeout);
        }
        onRequestTimeout = null;
        final long elapsedTime = getRequestElapsedTime();
        if (elapsedTime > WEBVIEW_SLOW_REQUEST_THRESHOLD) {
            reportSlowRequest(elapsedTime, url, errorCode);
        }
        clearRequestTime();
    }

    private void saveRequestStartTime() {...}
    private long getRequestElapsedTime() {...}
    private void clearRequestTime() {...}
    private void reportSlowRequest(long elapsedTime, String url, int errorCode) {...}
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文