gpt4 book ai didi

android - 使用 shouldInterceptRequest 阻止数据加载

转载 作者:太空狗 更新时间:2023-10-29 14:44:22 27 4
gpt4 key购买 nike

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

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

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

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

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

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

应用程序应始终显示相同的网页,不应加载任何数据,Play 商店 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 构建一个应用程序,因此我只能使用这个而不是接受请求作为第二个参数的那个。

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

最佳答案

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

关于android - 使用 shouldInterceptRequest 阻止数据加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42387313/

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