【问题标题】:Prevent data from loading using shouldInterceptRequest使用 shouldInterceptRequest 防止加载数据
【发布时间】:2017-02-22 09:20:03
【问题描述】:

虽然这个问题已经被问过好几次了,但与我的问题真正相关的唯一问题是这个 (Can I use shouldInterceptRequest to block a particular call in Android?),但它在 3 年多前被问到,最终没有得到答复。

我试图阻止的请求是一个 POST 请求,因此 shouldOverrideUrlLoading 方法无法拦截它。

单击按钮后,webview 会加载包含以下字符串的 URL:“androidBundleId_********”。

我需要阻止此请求并将用户重定向到 Play 商店(但我知道该怎么做!)

问题是 shouldInterceptRequest 方法的返回类型是 WebResourceResponse (哦,我多么希望它是布尔值!)。因此,我必须至少返回一个空白的 WebResourceResponse。

我不想这样做,因为 WebResourceResponse 中的所有内容都会加载到我的应用程序中。我只想让 webview 拦截请求并阻止它

应用应保持显示相同的网页,不应加载任何数据,除了 Play Store Intent。

我只发布我的代码的相关部分。

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    if(url.contains("androidBundleId_")){
        String bundleID = url.substring(url.indexOf("androidBundleId_")+"androidBundleId".length()+1, url.length());
        try {
            getContext().startActivity(new Intent
                    (Intent.ACTION_VIEW, Uri.parse("market://details?id=" + bundleID)));
        } catch (android.content.ActivityNotFoundException anfe) {
            getContext().startActivity(new Intent
                    (Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + bundleID)));
        }

        // I'D LIKE TO RETURN NOTHING HERE !!!
    }
    return super.shouldInterceptRequest(view, url);
}

我知道这个签名已被弃用,但我正在使用 minSdk 16 构建一个应用程序,因此我只能使用这个签名而不是接受 Request 作为第二个参数的那个。

你有什么解决方法吗?请注意,我无法更改包含该按钮的网页。

【问题讨论】:

    标签: android webview android-webview


    【解决方案1】:

    由于没有机会做我一直要求的事情,我们改变了整个应用程序逻辑并最终切换到 WebChromeClient。它自动处理链接/意图

    【讨论】:

    • 你知道WebResourceResponse类的等价物是什么吗?我的意思是像shouldInterceptRequest 这样工作但什么也不返回的东西。您的问题的确切解决方案是什么?
    • 请问您最终是如何使用 WebChromeClient 来阻止资源请求的?从 API 中看不出来。
    猜你喜欢
    • 2018-09-06
    • 2016-03-01
    • 1970-01-01
    • 2018-06-17
    • 2020-02-13
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 2021-09-20
    相关资源
    最近更新 更多