2016-03-29 21 views
2

Web sitesine erişmek için WebView kullanan uygulama oluşturuyorum. Sayfanın kullanılabilirliğini kontrol etmek için kod eklemek zorunda kaldım.Android webview özel hata sayfası

public class SpartanWeb extends Activity { 

WebView mWebView; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Adds Progrss bar Support 
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS); 
    setContentView(R.layout.main); 

    // Makes Progress bar Visible 
    getWindow().setFeatureInt(Window.FEATURE_PROGRESS, 
      Window.PROGRESS_VISIBILITY_ON); 

    // Get Web view 
    mWebView = (WebView) findViewById(R.id.webView1); 
    WebSettings websettings = mWebView.getSettings(); 
    websettings.setJavaScriptEnabled(true); 
    mWebView.stopLoading(); 
    mWebView.clearCache(true); 
    mWebView.loadUrl("http://google.com"); 
    mWebView.setHorizontalScrollBarEnabled(false); 
    mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); 
    mWebView.setWebViewClient(new WebViewClient()); 
    mWebView.setDownloadListener(new DownloadListener() { 
     public void onDownloadStart(String url, String userAgent, 
       String contentDisposition, String mimetype, 
       long contentLength) { 
      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setData(Uri.parse(url)); 
      startActivity(intent); 
     } 
    }); 

    // onProgressChanged 
    final Activity MyActivity = this; 
    mWebView.setWebChromeClient(new WebChromeClient() { 
     public void onProgressChanged(WebView view, int progress) { 
      // bar disappear after URL is loaded, and changes string to 
      // Loading... 
      MyActivity.setTitle("Loading..."); 
      MyActivity.setProgress(progress * 100); // Make the bar 
                // disappear after URL 
                // is loaded 

      // Return the app name after finish loading 
      if (progress == 100) 
       MyActivity.setTitle(R.string.app_name); 
     } 
    }); 

}// EOM oc 

public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { 
     mWebView.goBack(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

}

Ben onReceivedError ama nedense özel sayfası için yüklenmiyor eklemeye çalışıyorum.

/** Called when the activity is first created. */ 
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
{ 
mWebView.loadUrl("file:///android_asset/error.html"); 
} 

Lütfen ne yapmanız gerektiğini bildiriniz. Aşağıdaki kodu kullanabilirsiniz

+1

yeni (setWEbChromeClient – Jois

+0

daki onRecievedError uygulamak WebChromeClient() { \t \t \t public void onReceivedError (WebView görünüşüdür, int errorInfo string tanımı, dize failingUrl) \t \t \t {\t \t mWebView.loadUrl ("file: ///android_asset/error.html"); \t \t \t} 'hala varsayılan hata alma - Ben setWebViewClient ve çalışma :)' mWebView.setWebViewClient (yeni WebViewClient() {@Override public void onReceivedError \t \t \t (WebView görünümü, int errorCode eklenen – Ivan

+0

mevcut değil web sayfasını, Dize açıklama, Dize failingUrl) { \t \t \t mWebView.loadUrl ("file: ///android_asset/greska.html"); \t \t \t}}); soruya alamadım ' – Ivan

cevap

6

Sen onReceivedError işlevinde loadErrorPage (görünüm) işlevini çağırabilir.

Aşağıdaki kod, göstermeniz gereken hata içeriğini yükleyecektir. Html dosyasını loadDataWithBaseURL ile yüklüyorum.

public void loadErrorPage(WebView webview){ 
     if(webview!=null){ 

      String htmlData ="<html><body><div align=\"center\" >"This is the description for the load fail : "+description+"\nThe failed url is : "+failingUrl+"\n"</div></body>"; 

      webview.loadUrl("about:blank"); 
      webview.loadDataWithBaseURL(null,htmlData, "text/html", "UTF-8",null); 
      webview.invalidate(); 

     } 
    } 
+0

önce Bu, onReceivedError – Ivan

+1

eklemek için nasıl yardıma ihtiyacım var Bu kod ----> mWebView.setWebViewClient (yeni WebViewClient() { @Override public void onReceivedError (WebView görünümü, int errorCode, String açıklaması, String failingUrl) { Log.i ("WEB_VIEW_TEST", "hata kodu: "+ errorCode); loadErrorPage (görünüm); } }); ======= Bu size yardımcı olabilir. –

+0

Yardım için teşekkürler, kodumu mWebView.setWebViewClient (yeni WebViewClient() öğesine ekledim ve şimdi çalışıyor – Ivan

0

..

public class TestResultWebclient extends WebViewClient { 
     ProgressDialog progressDialog; 

     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      if (progressDialog == null) { 
       progressDialog = new ProgressDialog(TermsAndCondsMrupeeActivity.this); 
       progressDialog.setMessage("Loading..."); 
       progressDialog.show(); 
      } 
      super.onPageStarted(view, url, favicon); 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 

      view.loadUrl(url); 
      return true; 

     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      if (progressDialog != null) 
       try { 

        if (progressDialog.isShowing()) { 
         progressDialog.dismiss(); 
         progressDialog = null; 
        } 

       } catch (Exception exception) { 
        exception.printStackTrace(); 
       } 
      super.onPageFinished(view, url); 

     } 
    } 
+1

... –

2

i. Ipuçları için. Işe yarıyor şimdi teşekkürler mWebView.setWebViewClient için onReceivedError (yeni WebViewClient eklendi ben bu `mWebView.setWebChromeClient gibi onReceiveError koyarsanız

mWebView.setWebViewClient(new WebViewClient() { 
     @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       mWebView.loadUrl("file:///android_asset/error.html"); 
     } });