2014-07-01 46 views
0

我在我的應用中有一個WebView,但是當沒有互聯網連接時,它會顯示來自谷歌的正常網頁,上面寫着「沒有互聯網連接」。我想寧願它顯示兩件事:在android中使用webview和刷新按鈕加載

1)如果沒有互聯網連接,我想在那裏說「沒有互聯網連接」,並有一個刷新按鈕。

2)當應用程序啓動時,或者當它嘗試連接時,它會顯示一個進度條/加載屏幕。

下面是一些腳本網頁視圖:

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); 
    } 


} 

回答

1

創建自定義WebView類(創建類並將其擴展),並實現onReceivedError

報告錯誤到主機應用程序。這些錯誤是 不可恢復的(即主資源不可用)。 errorCode 參數對應於其中一個ERROR_ *常量。

參數 查看正在啓動回調的WebView。

errorCode對應於ERROR_ *值的錯誤代碼。

description描述錯誤的字符串。 failingUrl 無法加載的網址。

有,你可以處理這些錯誤:

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

+0

haa。我不明白的第一部分,我將添加到WebView代碼希望當沒有連接,它會告訴用戶沒有互聯網連接再試一次 – user3670592

+0

它應該是ERROR_CONNECT這是連接失敗時調用。 –

+0

好的,但對不起,我的意思是我應該在代碼中寫下它,以及如何。我的意思是編碼方式。看我的代碼並告訴我如何編輯它 – user3670592