Android Kaki

Build beautiful, usable products using required Components for Android.

Block Android WebView requests | by Ashish Yadav | Could 2023


A standard use case for intercepting internet requests is dealing with authentication. For instance, if a consumer is required to log in to view sure internet content material, the appliance can intercept the request and show the login display screen to the consumer. After the consumer enters their credentials, the appliance can modify the request and embrace the mandatory credentials earlier than sending it to the net server.

One other use case is loading native assets when a selected internet request is made. For instance, if the consumer has chosen a selected theme for the appliance, the appliance can intercept internet requests for the CSS file and cargo the native CSS file that matches that theme.

To intercept internet requests in Android WebView and cargo native recordsdata you should use WebViewClient.shouldInterceptRequest() technique and Asset Administration class. The shouldInterceptRequest technique is named for every useful resource requested by WebViewand it permits the appliance to course of the request. The Asset Administration class can be utilized to entry native recordsdata within the app’s property listing.

Right here is an instance of utilization shouldInterceptRequest() technique and Asset Administration class to load an area CSS file when a selected internet request is made:

WebView webView = findViewById(R.id.web_view);
webView.setWebViewClient(new WebViewClient() {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
// Examine if the request is for a selected useful resource
if (request.getUrl().getPath().equals("/css/types.css")) {
attempt {
// Load the native CSS file from the property folder
AssetManager assetManager = getAssets();
InputStream inputStream = assetManager.open("types.css");
return new WebResourceResponse("textual content/css", "UTF-8", inputStream);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
return tremendous.shouldInterceptRequest(view, request);
}
});
webView.loadUrl("https://instance.com");

On this instance the appliance is checking if the request is for a path /css/types.css and if it does, it’s going to load the native CSS file from the property folder, in any other case it’s going to return the default response.

Loading an area picture file:

if (request.getUrl().getPath().endsWith(".jpg")) {
attempt {
// Load the native picture file from the property folder
AssetManager assetManager = getAssets();
InputStream inputStream = assetManager.open("picture.jpg");
return new WebResourceResponse("picture/jpeg", "UTF-8", inputStream);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

Loading an area JavaScript file:

if (request.getUrl().getPath().endsWith(".js")) {
attempt {
// Load the native JavaScript file from the property folder
AssetManager assetManager = getAssets();
InputStream inputStream = assetManager.open("script.js");
return new WebResourceResponse("utility/javascript", "UTF-8", inputStream);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

Loading an area HTML file:

if (request.getUrl().getPath().endsWith("/about")) {
attempt {
// Load the native HTML file from the property folder
AssetManager assetManager = getAssets();
InputStream inputStream = assetManager.open("about.html");
return new WebResourceResponse("textual content/html", "UTF-8", inputStream);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

You’ll be able to see within the examples above that the appliance checks the URL path of the request to find out if it ought to load the file regionally. Then it makes use of Asset Administration class to open an area file from the property listing and return it as Net ResourcesFeedback.

It also needs to be famous that it is best to use shouldInterceptRequest() be cautious as it may well decelerate webview efficiency in the event you intercept too many requests or if the file you’re loading is giant.

Intercepting internet requests and loading native recordsdata generally is a highly effective instrument for customizing the habits of a WebView, but it surely must be used with care. It’s important to grasp what it means to course of requests and take a look at the appliance completely to make sure that it really works as anticipated.

John Wick: Chapter 4 (FREE) FULLMOVIE The Super Mario Bros Movie avatar 2 Where To Watch Creed 3 Free At Home Knock at the Cabin (2023) FullMovie Where To Watch Ant-Man 3 and the Wasp: Quantumania Cocaine Bear 2023 (FullMovie) Scream 6 Full Movie
Updated: May 30, 2023 — 2:07 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

androidkaki.com © 2023 Android kaki