ArnoldasM
ArnoldasM

Reputation: 206

get value of webWiev.getUrl() in WebResourceResponse shouldInterceptRequest(WebView view, String url)

I have function like this:

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

And it is working, fine. I want to add one check by current webView url and intercept request only if its on specific page. And when make this type of statetment:

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

application crashes. Could someone help me, and tell me what I'm doing wrong?

EDIT: Logs added.

 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)

Upvotes: 3

Views: 1963

Answers (1)

King of Masses
King of Masses

Reputation: 18765

You can try this:

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

As the warning says you are calling the webview methods in the WebViewCoreThread. Thus modify your code like this,

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

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

Upvotes: 2

Related Questions