系统崩溃时,在WebViewClient覆盖shouldInterceptRequest [英] System Crash When Overriding shouldInterceptRequest in WebViewClient

查看:1632
本文介绍了系统崩溃时,在WebViewClient覆盖shouldInterceptRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:

覆盖在一个web视图发出的所有请求,并要求自己(最终设置代理)。

Override all requests made by a WebView and make the request myself (eventually set up a proxy).

code:

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    if (url == null || url.trim().equals(""))
        return null;

    final DefaultHttpClient httpClient = new DefaultHttpClient();
    httpClient.getConnectionManager().closeExpiredConnections();
    final HttpUriRequest httpRequest = new HttpGet(url);

    try {
        final HttpResponse response = httpClient.execute(httpRequest);
        final Header[] headers = response.getHeaders(CONTENT_TYPE);
        String mimeType = "";
        String encoding = "";
        if (headers != null && headers.length > 0) {
            final String type = headers[0].getValue();
            final int semicolonIndex = type.indexOf(';');
            if (semicolonIndex != -1) {
                mimeType = type.substring(0, semicolonIndex).trim();
                encoding = type.substring(semicolonIndex + 1).trim();
                final int equalsIndex = encoding.indexOf('=');
                if (equalsIndex != -1)
                    encoding = encoding.substring(equalsIndex + 1).trim();
            } else
                mimeType = type;
        }

        return new WebResourceResponse(mimeType, encoding, response.getEntity().getContent());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } finally {
        httpClient.getConnectionManager().closeExpiredConnections();
    }
    return null;
}

的要求似乎都经历就好了,但最终我得到一个堆栈跟踪与以下两个问题之一:

The requests all seem to go through just fine, but eventually I get a stack trace with one of the two following issues:

3 15:07:28.650 E/InputDispatcher( 3981): channel '40d76268 com.secure.browser/com.secure.browser.SecureBrowserActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
01-03 15:07:28.650 E/InputDispatcher( 3981): channel '40d76268 com.secure.browser/com.secure.browser.SecureBrowserActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

这显然表明用完文件描述符的操作系统(FID)

which is apparently indicative of the operating system running out of file descriptors (fid)

01-03 15:29:36.810 I/DEBUG   ( 5798):     5903cd34  ac81c0b7  /system/lib/libdvm.so
01-03 15:29:38.380 I/DEBUG   ( 5798): debuggerd committing suicide to free the zombie!
01-03 15:29:38.380 I/BootReceiver( 3981): Copying /data/tombstones/tombstone_07 to DropBox 

(SYSTEM_TOMBSTONE)

(SYSTEM_TOMBSTONE)

这意味着我认为意味着OS运行到较低层次的问题。

Which means I think means that the OS is running into low level issues.

我使用的3.0 +这样的功能应该得到支持。

I am using 3.0 + so the function should be supported.

当我打开使用上的JavaScript,或浏览了没有JavaScript的一段时间后,这大多是失败。

This mostly fails when I turn javascript on, or after browsing for a while without javascript.

推荐答案

我只有一个评论,我看到,在开始的时候你返回null如果该URL为null或者为空或只是空格,但在<一个href="http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldInterceptRequest%28android.webkit.WebView,%20java.lang.String%29"相对=nofollow>文档你可以阅读返回 一个WebResourceResponse包含响应信息,或如果web视图应该加载资源本身无效。,所以如果这是你想要继续前进,如果没有,我会建议super.shouldInterceptRequest(看来,什么)可能会导致空网址造成一些麻烦。

I just have one comment, i see that at the beginning you return null if the URL is null or if is empty or just spaces, but on the documentation you can read "Returns A WebResourceResponse containing the response information or null if the WebView should load the resource itself." so if that is what you want go ahead if not i would recommend super.shouldInterceptRequest(view, "") cause that null URL maybe is causing some troubles.

这篇关于系统崩溃时,在WebViewClient覆盖shouldInterceptRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆