在WebResourceResponse中获取webWiev.getUrl()的值shouldInterceptRequest(WebView视图,String url)

时间:2015-05-08 06:56:36

标签: android webview android-webview

我的功能如下:

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网址添加一个检查,并且只有在特定页面上才能拦截请求。当做出这种类型的陈述时:

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)

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

  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);  
     }
});