我有这样的功能:

public WebResourceResponse shouldInterceptRequest(WebView view, String url) {

            if (url.contains("images/srpr/logo11w.png")){
                return new WebResourceResponse("text/plain", "utf-8",
                        new ByteArrayInputStream("".getBytes()));
            }

            return super.shouldInterceptRequest(view, url);
}


而且工作正常,很好。我想通过当前的webView url添加一项检查,并且仅在特定页面上时才拦截请求。当做出这种陈述时:

public WebResourceResponse shouldInterceptRequest(WebView view, String url) {

            if (url.contains("images/srpr/logo11w.png") && webView.getUrl().contains("google.lt")){
                return new WebResourceResponse("text/plain", "utf-8",
                        new ByteArrayInputStream("".getBytes()));
            }

            return super.shouldInterceptRequest(view, url);
        }


应用程序崩溃。有人可以帮我,告诉我我做错了什么吗?

编辑:添加的日志。

 1689-1731/com.application.webview W/WebView﹕ java.lang.Throwable: A WebView method was called on thread 'Thread-102'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {527c3a60} called on null, FYI main Looper is Looper (main, tid 1) {527c3a60})
            at android.webkit.WebView.checkThread(WebView.java:2072)
            at android.webkit.WebView.getUrl(WebView.java:1229)
            at com.application.webview.fragment.MainFragment$3.shouldInterceptRequest(MainFragment.java:452)
            at com.android.webview.chromium.WebViewContentsClientAdapter.shouldInterceptRequest(WebViewContentsClientAdapter.java:283)
            at com.android.org.chromium.android_webview.AwContents$IoThreadClientImpl.shouldInterceptRequest(AwContents.java:244)
            at dalvik.system.NativeStart.run(Native Method)
05-08 03:12:12.023    1689-1731/com.application.webview W/System.err﹕ java.lang.RuntimeException: java.lang.Throwable: A WebView method was called on thread 'Thread-102'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {527c3a60} called on null, FYI main Looper is Looper (main, tid 1) {527c3a60})
05-08 03:12:12.027    1689-1731/com.application.webview W/System.err﹕ at android.webkit.WebView.checkThread(WebView.java:2082)
05-08 03:12:12.031    1689-1731/com.application.webview W/System.err﹕ at android.webkit.WebView.getUrl(WebView.java:1229)
05-08 03:12:12.035    1689-1731/com.application.webview W/System.err﹕ at com.application.webview.fragment.MainFragment$3.shouldInterceptRequest(MainFragment.java:452)
05-08 03:12:12.043    1689-1731/com.application.webview W/System.err﹕ at com.android.webview.chromium.WebViewContentsClientAdapter.shouldInterceptRequest(WebViewContentsClientAdapter.java:283)
05-08 03:12:12.047    1689-1731/com.application.webview W/System.err﹕ at com.android.org.chromium.android_webview.AwContents$IoThreadClientImpl.shouldInterceptRequest(AwContents.java:244)
05-08 03:12:12.051    1689-1731/com.application.webview W/System.err﹕ at dalvik.system.NativeStart.run(Native Method)
05-08 03:12:12.111    1689-1731/com.application.webview W/System.err﹕ Caused by: java.lang.Throwable: A WebView method was called on thread 'Thread-102'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {527c3a60} called on null, FYI main Looper is Looper (main, tid 1) {527c3a60})
05-08 03:12:12.127    1689-1731/com.application.webview W/System.err﹕ at android.webkit.WebView.checkThread(WebView.java:2072)
05-08 03:12:12.127    1689-1731/com.application.webview W/System.err﹕ ... 5 more
05-08 03:12:12.175    1689-1731/com.application.webview A/libc﹕ Fatal signal 6 (SIGABRT) at 0x00000699 (code=-6), thread 1731 (Chrome_IOThread)

最佳答案

您可以尝试以下方法:

  public WebResourceResponse shouldInterceptRequest(WebView view, String url) {

            if (url.contains("images/srpr/logo11w.png") && Uri.parse(url).getHost().equals("www.google.com")){
                return new WebResourceResponse("text/plain", "utf-8",
                        new ByteArrayInputStream("".getBytes()));
            }

            return super.shouldInterceptRequest(view, url);
        }


如警告所述,您正在WebViewCoreThread中调用webview方法。这样修改您的代码,

YourActivity.this.runOnUiThread(new Runnable() {
    public void run() {

              // do all web view operations here
        webView.loadUrl(Path);
     }
});

10-07 12:46