2013-05-17 166 views
0

我有Android應用程序,它使用webview來顯示Google App Engine網絡應用程序。如何覆蓋在我的應用程序中遇到的默認HTTP Error 504 Gateway timeoutAndroid Webview - HTTP錯誤504網關超時

HTTP Error 504 Gateway timeout 

The server, while acting as a gateway or proxy, did not receive a 
timely response from the upstream server it accessed in attempting 
to complete the request. 

我已經覆蓋onReceivedError哪些工作時沒有互聯網連接可用和其他錯誤。

webView.setWebViewClient(new WebViewClient(){ 
    ... 

    @SuppressLint("DefaultLocale") 
    @Override 
    public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) { 

     try { 
      String template = streamToString(getAssets().open("html/error.html")); 
      String data = template.replaceAll("%DESCRIPTION%", description.toLowerCase()) 
        .replaceAll("%WEBSITE_URL%", WEBSITE_URL); 

      view.loadDataWithBaseURL("file:///android_asset/html/", data, "text/html", "utf-8", null); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

}); 

onReceivedError只能收到HTTP錯誤網絡錯誤?任何解決方法? android webview如何攔截HTTP錯誤?

回答

0

其實你的要求越來越低,因爲連接或網絡相關的原因的超時..你可以顯示自定義頁面這樣Inshallah

webview.setWebViewClient(new WebViewClient() { 
      /* (non-Javadoc) 
      * @see android.webkit.WebViewClient#onPageStarted(android.webkit.WebView, java.lang.String, android.graphics.Bitmap) 
      */ 
      @Override 
      public void onPageStarted(WebView view, String url, Bitmap favicon) { 
       System.out.println("page loading started"); 
       // TODO Auto-generated method stub 
       if(!isNetworkAvailable2()) 
       { 
        //showInfoMessageDialog("network not available"); 
        //load here your custom offline page for handling such errors 

        System.out.println("network not available"); 
        return; 
       } 
       else System.out.println("network available"); 

       super.onPageStarted(view, url, favicon); 

      } 

      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       isConnected=isNetworkAvailable2(); 
       if (isConnected) { 
        // return false to let the WebView handle the URL 
        return false; 
       } else { 
        // show the proper "not connected" message 
       // view.loadData(offlineMessageHtml, "text/html", "utf-8"); 
        // return true if the host application wants to leave the current 
        // WebView and handle the url itself 
        return true; 
       } 
      } 
      @Override 
      public void onReceivedError (WebView view, int errorCode, 
       String description, String failingUrl) { 
       if (errorCode == ERROR_TIMEOUT) { 
        view.stopLoading(); // may not be needed 
       // view.loadData(timeoutMessageHtml, "text/html", "utf-8"); 
       } 
      } 
     }); 
     //webview.setWebChromeClient(new WebChromeClient());   
    } 

Credits to the original answer here.

+0

'onReceivedError'不當錯誤發生時調用,而是顯示一個默認的錯誤頁面。 –

+0

這是否會一直髮生?\ – Nezam

+0

現在試試我的編輯 – Nezam