使用shouldInterceptRequest阻止数据加载 [英] Prevent data from loading using shouldInterceptRequest

查看:307
本文介绍了使用shouldInterceptRequest阻止数据加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管已多次询问,但与我的问题真正相关的唯一问题是(

While this has been asked several times, the only question that really relates to my issue is this (Can I use shouldInterceptRequest to block a particular call in Android?), but it was asked more than 3 years ago and eventually got no replies.

我要阻止的请求是POST请求,因此shouldOverrideUrlLoading方法无法拦截它.

The request I'm trying to block is a POST request, so the shouldOverrideUrlLoading method is unable to intercept it.

点击按钮后,Web视图将加载包含以下字符串的URL:"androidBundleId _ ********".

After a button is clicked, the webview loads an URL that contains the following string: "androidBundleId_********".

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

I need to block this request and redirect users to the Play Store (but I know how to do this!)

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

The problem is that the return type of the shouldInterceptRequest method is WebResourceResponse (oh, how I wish it was boolean!). Therefore, I must return at least a blank WebResourceResponse.

我不想,因为WebResourceResponse内部的所有内容都已加载到我的应用程序中.我只想让Webview拦截请求并阻止.

I don't want to, as everything that is inside the WebResourceResponse gets loaded in my app. I just want the webview to intercept the request and block it.

除了Play商店的意图外,该应用程序应继续显示相同的网页,不得加载任何数据.

The app should keep showing the same webpage, no data should be loaded, other than the Play Store intent.

我只发布了代码的相关部分.

I'm posting only the relevant part of my code.

@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作为第二个参数的那个

I know this signature is deprecated, but I'm building an app with minSdk 16, therefore I can only use this one and not the one accepting Request as second parameter.

您有任何解决方法吗?请注意,我无法更改包含该按钮的网页.

Do you have any workaround? Note that I cannot change the webpage containing the button.

推荐答案

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

As there is no chance to do what I've been asking for, we changed the whole application logic and eventually switched to WebChromeClient. It handles links / intents automatically

这篇关于使用shouldInterceptRequest阻止数据加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆