Intercept and override HTTP requests from WebView
Solution 1
It looks like API level 11 has support for what you need. See WebViewClient.shouldInterceptRequest()
.
Solution 2
Try this, I've used it in a personal wiki-like app:
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("foo://")) {
// magic
return true;
}
return false;
}
});
Solution 3
As far as I know, shouldOverrideUrlLoading is not called for images but rather for hyperlinks... I think the appropriate method is
@Override
public void onLoadResource(WebView view, String url)
This method is called for every resource (image, styleesheet, script) that's loaded by the webview, but since it's a void, I haven't found a way to change that url and replace it so that it loads a local resource ...
Solution 4
Here is my solution I use for my app.
I have several asset folder with css / js / img anf font files.
The application gets all filenames and looks if a file with this name is requested. If yes, it loads it from asset folder.
//get list of files of specific asset folder
private ArrayList listAssetFiles(String path) {
List myArrayList = new ArrayList();
String [] list;
try {
list = getAssets().list(path);
for(String f1 : list){
myArrayList.add(f1);
}
} catch (IOException e) {
e.printStackTrace();
}
return (ArrayList) myArrayList;
}
//get mime type by url
public String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
if (extension.equals("js")) {
return "text/javascript";
}
else if (extension.equals("woff")) {
return "application/font-woff";
}
else if (extension.equals("woff2")) {
return "application/font-woff2";
}
else if (extension.equals("ttf")) {
return "application/x-font-ttf";
}
else if (extension.equals("eot")) {
return "application/vnd.ms-fontobject";
}
else if (extension.equals("svg")) {
return "image/svg+xml";
}
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
//return webresourceresponse
public WebResourceResponse loadFilesFromAssetFolder (String folder, String url) {
List myArrayList = listAssetFiles(folder);
for (Object str : myArrayList) {
if (url.contains((CharSequence) str)) {
try {
Log.i(TAG2, "File:" + str);
Log.i(TAG2, "MIME:" + getMimeType(url));
return new WebResourceResponse(getMimeType(url), "UTF-8", getAssets().open(String.valueOf(folder+"/" + str)));
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
//@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@SuppressLint("NewApi")
@Override
public WebResourceResponse shouldInterceptRequest(final WebView view, String url) {
//Log.i(TAG2, "SHOULD OVERRIDE INIT");
//String url = webResourceRequest.getUrl().toString();
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
//I have some folders for files with the same extension
if (extension.equals("css") || extension.equals("js") || extension.equals("img")) {
return loadFilesFromAssetFolder(extension, url);
}
//more possible extensions for font folder
if (extension.equals("woff") || extension.equals("woff2") || extension.equals("ttf") || extension.equals("svg") || extension.equals("eot")) {
return loadFilesFromAssetFolder("font", url);
}
return null;
}
Solution 5
An ultimate solution would be to embed a simple http server listening on your 'secret' port on loopback. Then you can substitute the matched image src URL with something like http://localhost:123/.../mypic.jpg
Olegas
Web development, Android development. NodeJS, JavaScript, Java.
Updated on January 09, 2021Comments
-
Olegas over 3 years
I have a WebView in my application in which some site is opened (always the same, it is my own page). The site has some JS code that loads some images from the remote host.
I want to intercept requests to such images (by URL pattern) and give back my own content (i.e. another image), or leave request untouched depending on internal application logic.
Is it possible to do that?
EDIT: The current state of the question...
WebView
has the ability to set aWebViewClient
(as noted by Axarydax).WebViewClient
have two useful methodsshouldOverrideUrlLoading
onLoadResource
shouldOverrideUrlLoading
is able to intercept any URL loading, if loading is triggered by page interaction (i.e. link on a page is clicked, WebView.loadURL("") isn't triggering this method). It is also able to cancel URL loading by returning false. This approach isn't usable, cause' it is not able to intercept loading of page resources (and images, what I need to intercept, is such a page resource).onLoadResource
is triggering every time that page resource (and images! thx to Jessyjones) are loading, but there is no way to cancel that. That makes this method not suitable for my task also. -
Olegas over 13 yearsOk, it is the way to intercept call (by the way, will it trigger if my page loads an image, not doing a full reload?), but how can i supply response for the intercepted call produced by my Java code?
-
Olegas over 13 yearsYes, with this method i got all the urls loaded (a root one, and all resources there). But still the question is - how to override loading =( I have some ideas on how to replace image content, but still it isn't clear how to override original call.
-
Axarydax over 13 yearsyou can use loadData() method of WebView to push some html into it. Usage: webView.loadData("<html>...</html>", "text/html", "utf-8");
-
Olegas over 13 yearsNot suitable. Static content loaded through loadData haven't access to additional content loading throug network. As a workaround loadData("javascript:....") can be used (to interact with some "client" code). This can be a good and suitable aproach if shouldOverrideUrlLoading is called for every page resource (content images exactly). But it didn't.
-
gnclmorais about 13 yearsWhat about if you use
url.endsWith("jpg")
? You just have to use it to jpeg, gif and png... Then, the code Azarydax provided should work, no? -
Olegas about 13 years@MEGA shouldOverrideUrlLoading is NOT called for page resources (images are among them)
-
ADDruid about 10 yearsA word of CAUTION! If you intercept an attempt by the browser to retrieve an image (or probably any resource) and then return
null
(meaning let the WebView continue retrieving the image), any future requests to this resource might just go to the cache and will NOT triggershouldInterceptRequest()
. If you want to intercept EVERY image request, you need to disable the cache or (what I did) callWebView.clearCache(true)
. -
Francisco Corrales Morales almost 10 years
The method ShouldOverrideUrlLoading(WebView, String) of type new WebViewClient(){} must override or implement a supertype method
-
Francisco Corrales Morales almost 10 yearsand what about inspection ? (stackoverflow.com/questions/24000805/…)
-
WCG about 8 yearsThe "text/javascript" like mimeType reference here: stackoverflow.com/questions/8589645/…
-
narancs almost 7 yearsI am trying to add custom cookies and additional headers. Any idea how that could be done?
-
Jonik almost 7 yearsSince API 24, this version of
shouldOverrideUrlLoading
is deprecated. To deal with that, see this question: stackoverflow.com/q/36484074/56285 -
Jonik almost 7 yearsSince API 24, the version of
shouldInterceptRequest
that takesString url
is deprecated. To deal with that, see this question: stackoverflow.com/q/36484074/56285 (With regards to the deprecation,shouldInterceptRequest
is analogous toshouldOverrideUrlLoading
.) -
Miloš Černilovský almost 5 yearsThis works for the specific case in the question, but is not universal for all network calls, because the shouldInterceptRequest() method does not provide POST requests' data.