【问题标题】:How to check url is loaded in webview or not如何检查 url 是否在 webview 中加载
【发布时间】:2012-01-11 06:26:18
【问题描述】:

我正在使用以下代码在 android webview 中加载 url

webviewShowPost.loadUrl(URL);

我想检查是否没有可用的数据连接然后 webview 而不是显示空白视图,我可以显示没有连接的 Toast。

谢谢

【问题讨论】:

    标签: android url webview connectivity


    【解决方案1】:
    public static boolean isOnline(Context context) {
    
            try {
                ConnectivityManager cm = (ConnectivityManager) context
    
                .getSystemService(Context.CONNECTIVITY_SERVICE);
    
                if (cm.getActiveNetworkInfo().isConnectedOrConnecting()) {
    
                    URL url = new URL("http://www.google.com.pk/");
                                    HttpURLConnection urlc = (HttpURLConnection) url
                            .openConnection();
                    urlc.setConnectTimeout(1000); // mTimeout is in seconds
    
                    urlc.connect();
    
                    if (urlc.getResponseCode() == 200) {
                        return true;
                    } else {
                        return false;
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return false;
        }
    

    【讨论】:

    • 此代码检查来自服务器的连接和响应它比连接管理器更可靠,因为在某些情况下 wifi 已启用但无法连接到服务器或无法获得任何响应。谢谢。
    【解决方案2】:

    你想在加载页面之前检查是否有网络连接,这意味着你想这样做:https://stackoverflow.com/a/2001824/960048

    【讨论】:

      【解决方案3】:

      您始终可以为此目的使用 WebViewClient。

          web.setWebViewClient(new WebViewClient(){
      
               public void onReceivedError(WebView view, int errorCode, String description,String failingUrl) {
                   Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
               }
          });
      

      【讨论】:

        【解决方案4】:

        您可以在上面的 webview 方法中获取 Progress Percentage 值

         mWebView.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress)  
                {
                 //Make the bar disappear after URL is loaded, and changes string to Loading...
             //Make the bar disappear after URL is loaded
                 System.out.println("Value of progress"+progress);
                    pbweb.setProgress(progress);
        
                    if(progress == 100)
        
        
                    pbweb.setVisibility(View.GONE);
                  }
                });
        

        下面的代码是progerss的价值

        【讨论】:

        【解决方案5】:

        请试试这个

        webView.setWebViewClient(new WebViewClient() { 
                @Override
                public void onReceivedError(WebView view, int errorCode,
                        String description, String failingUrl) {
                    // TODO Auto-generated method stub
                    super.onReceivedError(view, errorCode, description, failingUrl);
                }
                @Override
                public void onPageFinished(WebView view, String url) {
                    // TODO Auto-generated method stub
                    super.onPageFinished(view, url);
                }
        
        
            });
        

        【讨论】:

        • 似乎在出现错误时也会调用 onPageFinished(例如,没有互联网连接)。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多