【问题标题】:Android Webview - HTTP Error 504 Gateway timeoutAndroid Webview - HTTP 错误 504 网关超时
【发布时间】:2013-05-12 03:41:34
【问题描述】:

我有一个使用 webview 来显示 Google App Engine 网络应用程序的 android 应用程序。如何覆盖我的应用中遇到的默认HTTP Error 504 Gateway timeout

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错误?

【问题讨论】:

    标签: android google-app-engine error-handling android-webview


    【解决方案1】:

    实际上您的请求由于连接性低或网络相关原因而超时;您可以通过这种方式显示自定义页面:

      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.

    【讨论】:

    • onReceivedError 在发生错误时未被调用,而是显示默认错误页面。
    • 这种情况经常发生吗?\
    • 我必须收到反馈,当用户的互联网连接速度非常慢时会发生这种情况
    • 我的意思是我刚刚收到反馈并收到错误的屏幕截图
    • 对不起,我没听明白。您需要通过在if(!isNetworkAvailable2()){块内编写相关代码来处理错误
    猜你喜欢
    • 2018-09-13
    • 2020-06-03
    • 2011-04-08
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 2015-10-20
    相关资源
    最近更新 更多