使用 shouldInterceptRequest 阻止数据加载
Prevent data from loading using shouldInterceptRequest
虽然这个问题已经被问过好几次了,但真正与我的问题相关的唯一问题是这个(Can I use shouldInterceptRequest to block a particular call in Android?),但它在 3 年多前就被问过,最终没有得到回复。
我要阻止的请求是 POST 请求,因此 shouldOverrideUrlLoading 方法无法拦截它。
单击按钮后,webview 加载包含以下字符串的 URL:"androidBundleId_********".
我需要阻止此请求并将用户重定向到 Play 商店(但我知道该怎么做!)
问题是 shouldInterceptRequest 方法的 return 类型是 WebResourceResponse (哦,我多么希望它是布尔值!).因此,我必须 return 至少一个空白的 WebResourceResponse。
我不想,因为 WebResourceResponse 中的所有内容都会加载到我的应用程序中。我只是想让 webview 拦截请求并阻止它。
该应用应始终显示相同的网页,除 Play 商店意图外,不应加载任何数据。
我只发布我的代码的相关部分。
@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。它自动处理链接/意图
虽然这个问题已经被问过好几次了,但真正与我的问题相关的唯一问题是这个(Can I use shouldInterceptRequest to block a particular call in Android?),但它在 3 年多前就被问过,最终没有得到回复。
我要阻止的请求是 POST 请求,因此 shouldOverrideUrlLoading 方法无法拦截它。
单击按钮后,webview 加载包含以下字符串的 URL:"androidBundleId_********".
我需要阻止此请求并将用户重定向到 Play 商店(但我知道该怎么做!)
问题是 shouldInterceptRequest 方法的 return 类型是 WebResourceResponse (哦,我多么希望它是布尔值!).因此,我必须 return 至少一个空白的 WebResourceResponse。
我不想,因为 WebResourceResponse 中的所有内容都会加载到我的应用程序中。我只是想让 webview 拦截请求并阻止它。
该应用应始终显示相同的网页,除 Play 商店意图外,不应加载任何数据。
我只发布我的代码的相关部分。
@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。它自动处理链接/意图