【问题标题】:loading in android with webview and refresh button使用 webview 和刷新按钮加载 android
【发布时间】:2014-08-22 13:18:00
【问题描述】:

我的应用中有一个 WebView,但是当没有互联网连接时,它会显示来自谷歌的正常网页,上面写着“没有互联网连接”。我宁愿它显示两件事:

1) 如果没有互联网连接,我想说“没有互联网连接”并有一个刷新按钮。

2) 当应用程序启动或尝试连接时,它会显示一个进度条/加载屏幕。

这是一些 WebView 脚本:

 WebView wv = (WebView) view.findViewById(R.id.webViewF);

        wv.getSettings().setJavaScriptEnabled(true);

        String url = "http://m.facebook.com/";
        wv.getSettings().setJavaScriptEnabled(true);
        wv.loadUrl(url);
        wv.setWebChromeClient(new WebChromeClient()); 
        wv.setWebViewClient(new WebViewClient());





public class myWebClient extends WebViewClient
{


    @Override

    public void onReceivedError(WebView view, int errorCode,
            String description, String failingUrl) {
         if (errorCode == ERROR_CONNECT)
         {
              What should i write here ??!!!

         }
             // TODO Auto-generated method stub
        super.onReceivedError(view, errorCode, description, failingUrl);
    }


}

【问题讨论】:

    标签: android webview


    【解决方案1】:

    创建一个自定义的WebView 类(创建一个类并扩展它)并实现onReceivedError

    向宿主应用程序报告错误。这些错误是 不可恢复(即主要资源不可用)。错误代码 参数对应于 ERROR_* 常量之一。

    参数 view 发起回调的 WebView。

    errorCode ERROR_* 值对应的错误代码。

    description 描述错误的字符串。 failedUrl 的 url 加载失败。

    您可以处理以下错误:

    int ERROR_AUTHENTICATION    User authentication failed on server
    int ERROR_BAD_URL   Malformed URL
    int ERROR_CONNECT   Failed to connect to the server
    int ERROR_FAILED_SSL_HANDSHAKE  Failed to perform SSL handshake
    int ERROR_FILE  Generic file error
    int ERROR_FILE_NOT_FOUND    File not found
    int ERROR_HOST_LOOKUP   Server or proxy hostname lookup failed
    int ERROR_IO    Failed to read or write to the server
    int ERROR_PROXY_AUTHENTICATION  User authentication failed on proxy
    int ERROR_REDIRECT_LOOP Too many redirects
    int ERROR_TIMEOUT   Connection timed out
    int ERROR_TOO_MANY_REQUESTS Too many requests during this load
    int ERROR_UNKNOWN   Generic error
    int ERROR_UNSUPPORTED_AUTH_SCHEME   Unsupported authentication scheme (not basic or digest)
    int ERROR_UNSUPPORTED_SCHEME    Unsupported URI scheme
    

    对于你的 #2 事情,使用 onPageStartedonPageFinished

    【讨论】:

    • 哈哈。我不明白我将添加到 webview 代码的第一部分希望在没有连接时它会告诉用户没有互联网连接重试
    • 应该是连接失败时调用的ERROR_CONNECT。
    • 好的,但对不起,我的意思是我应该在代码中的哪个位置以及如何编写它。我的意思是编码方式。查看我的代码并告诉我如何编辑它
    • 正如我所说,您应该创建另一个扩展 WebView 并覆盖该方法的类。
    • 1.现在你应该开始使用 myWebClient 作为 View 而不是默认的 WebView。 2.什么int ERROR_CONNECT;方法?如果 (errorCode == WHAT_ERROR_YOU_WANT_HANDLE) { /* 处理它 */ } 你应该做点什么
    猜你喜欢
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 2019-03-05
    相关资源
    最近更新 更多