【问题标题】:What to do with an IOException in WebViewClient.shouldInterceptRequest()如何处理 WebViewClient.shouldInterceptRequest() 中的 IOException
【发布时间】:2016-01-22 10:56:56
【问题描述】:

我正在尝试拦截来自 WebView 的请求,以便注入额外的标头。我正在将 WebViewClient 应用到 WebView 并覆盖 shouldInterceptRequest()

shouldInterceptRequest() 中,我打开连接,添加标头,然后在 WebResourceResponse 中返回打开的流。

如果连接的初始打开失败,我不清楚应该如何处理 IOException。

final Map<String, String> extraHeaders = getExtraHeaders(intent);
webview.setWebViewClient(new WebViewClient() {
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        final Uri uri = request.getUrl();

        try {
            URL           url = new URL(uri.toString());
            URLConnection con = url.openConnection();
            for (Map.Entry<String, String> h : extraHeaders.entrySet()) {
                con.addRequestProperty(h.getKey(), h.getValue());
            }
            final String contentType = con.getContentType().split(";")[0];
            final String encoding    = con.getContentEncoding();
            return new WebResourceResponse(contentType, encoding, con.getInputStream());
        } catch (IOException e) {
            // what should we do now?
            e.printStackTrace();
        }

        return super.shouldInterceptRequest(view, request);
    }
});

我不能让它不被发现,因为它是一个已检查的异常并且不是shouldInterceptRequest() 签名的一部分。

我不能将它包装在未经检查的异常中,因为它不会被 WebView 捕获并杀死应用程序。

如果我捕获并忽略异常,并默认使用 super 方法(它只返回 null),那么 WebView 将继续其默认行为并尝试发送请求(没有我的额外标头)。这是不可取的,因为 WebView 自己的连接尝试实际上可能会成功,而缺少的 headers 会导致更多的问题。

似乎没有办法表明拦截失败,应该中止请求。

这里最好做什么?


我已尝试返回模拟失败响应,但这不会被视为错误。 WebView 显示一个包含响应内容(来自异常的错误消息)的页面,并且未调用 WebViewClient 的 onReceivedError()onReceivedHttpError() 回调。

} catch (IOException e) {
    InputStream is = new ByteArrayInputStream(e.getMessage().getBytes());
    return new WebResourceResponse("text/plain", "UTF-8", 500, "Intercept failed",
                                   Collections.<String, String>emptyMap(),
                                   is);
}

【问题讨论】:

    标签: android android-webview httprequest ioexception


    【解决方案1】:

    您可以尝试实例化一个新的WebResourceError,然后调用WebViewClient.onReceivedError。从来没有尝试过这样的事情,但这就是它的样子:

    } catch (final IOException e) {
        WebResourceError wre = new WebResourceError(){
            @Override
            public CharSequence getDescription (){
                return e.toString(); 
            }
    
            @Override
            public int getErrorCode (){
                return WebViewClient.ERROR_CONNECT;
            }
        };
    
        onReceivedError(view, request, wre);
    
        return true;
    }
    

    如果您不想或无法实例化WebResourceError,也可以使用deprecated version of onReceivedError

    【讨论】:

      【解决方案2】:

      表示发生了某种 I/O 异常。此类是由失败或中断的 I/O 操作产生的一般异常类。

      IOException() 构造一个以 null 作为其错误详细消息的 IOException。

      IOException(字符串消息) 构造一个带有指定详细消息的 IOException。

      IOException(字符串消息,可抛出原因) 使用指定的详细消息和原因构造一个 IOException。

      IOException(可抛出的原因) 构造一个带有指定原因和详细消息的 IOException (cause==null ? null : cause.toString()) (which ty picly 包含原因的类别和详细信息)。

      你有关于 IOException 的例子

      这里:

      http://examples.javacodegeeks.com/core-java/io/ioexception/java-io-ioexception-how-to-solve-ioexception/

      【讨论】:

      • 这没有回答问题。我知道 IOException 是什么。
      猜你喜欢
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      • 2012-02-27
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多