gpt4 book ai didi

android - 在 WebResourceResponse 中获取 webWiev.getUrl() 的值 shouldInterceptRequest(WebView view, String url)

转载 作者:行者123 更新时间:2023-11-29 01:33:10 24 4
gpt4 key购买 nike

我有这样的功能:

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

关于android - 在 WebResourceResponse 中获取 webWiev.getUrl() 的值 shouldInterceptRequest(WebView view, String url),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30117621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com