问题
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.
The request I'm trying to block is a POST request, so the shouldOverrideUrlLoading method is unable to intercept it.
After a button is clicked, the webview loads an URL that contains the following string: "androidBundleId_********".
I need to block this request and redirect users to the Play Store (but I know how to do this!)
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.
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.
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);
}
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.
回答1:
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
来源:https://stackoverflow.com/questions/42387313/prevent-data-from-loading-using-shouldinterceptrequest