【问题标题】:WebView not receiving error even if server sends back an error即使服务器发回错误,WebView 也没有收到错误
【发布时间】:2011-04-15 20:26:20
【问题描述】:

我尝试访问的网页发回了 403 错误。我期待 WebViewClient 收到错误,但事实并非如此。不应该报错吗?

engine.setWebViewClient(new WebViewClient() {
        boolean error = false;
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {

            //IT DOESN'T COME HERE even IF SERVER SENDS BACK 403 ERROR
        }
    });

看起来,我必须使用 HttpRequest 读取响应并解析响应以检查发生了什么。有没有其他方法可以做到这一点?我可以从 WebView 中检查标题吗?

【问题讨论】:

    标签: android http-headers webview


    【解决方案1】:

    我必须使用两个请求来实现该要求。第一次请求检查凭据是否正常并且没有错误(使用 HttpURLConnection)。只有当响应正常时,我才会加载 webview。

    protected Void doInBackground(final String... url) {
            boolean failed = false;
            int error = 0;
    
            try {
                URL urlToLoad = new URL(url[0]);
                HttpURLConnection conn = (HttpURLConnection) urlToLoad
                        .openConnection();
                int response = conn.getResponseCode();
                if (response != HttpURLConnection.HTTP_OK) {
                    error = response;
                    failed = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (!failed) {
                webView.loadUrl(url[0]);
            } else {
                handler.post(hideProgressRunnable);
                connecting = false;
                showError(ErrorMap.getErrorForHttpCode(error));
            }
            return null;
        }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-02
    • 2021-07-24
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 2021-06-10
    相关资源
    最近更新 更多