Requirement background
To receive such a requirement, you need to add a flag with xxx=1 to the requested url in all network requests in the WebView.
For example, adding the flag bit becomes ?xxx=1
Find a solution
Starting from Android API 11 (3.0), WebView has provided such an API in the WebViewClient, as follows:
public WebResourceResponse shouldInterceptRequest(WebView view, String url)
That is to say, just implement the shouldInterceptRequest method of WebViewClient and then call the setWebViewClient of WebView.
However, the above API is deprecated above API21 and a new API is used, as follows:
public WebResourceResponse shouldInterceptRequest(WebView view, final WebResourceRequest request)
Well, in order to support as many versions as possible, it seems that both need to be implemented. I found that the String url, which is very useful at first glance, has become a WebResourceRequest request. WebResourceRequest This thing is an interface and is defined like this:
public interface WebResourceRequest { Uri getUrl(); boolean isForMainFrame(); boolean hasGesture(); String getMethod(); Map<String, String> getRequestHeaders(); }
No method was found in it that could directly replace the request.
Then I searched for the reference to him in the Android code and clicked me to search. Then I found that private static class WebResourceRequestImpl implements WebResourceRequest Its internal implementation is just a simple entity. Then it will be very easy to replace this thing, and you can do all three methods:
- Dynamic Agent
- reflection
- Re-implement
accomplish
The plan is determined, and the rest is simple. Directly upload the code.
First, add the flag bit to the URL string
public static String injectIsParams(String url) { if (url != null && !("xxx=") { if (("?")) { return url + "&xxx=1"; } else { return url + "?xxx=1"; } } else { return url; } }
Then all requests are intercepted
(new WebViewClient() { @SuppressLint("NewApi") @Override public WebResourceResponse shouldInterceptRequest(WebView view, final WebResourceRequest request) { if (request != null && () != null) { String scheme = ().getScheme().trim(); if (("http") || ("https")) { return (view, new WebResourceRequest() { @Override public Uri getUrl() { return (injectIsParams(().toString())); } @SuppressLint("NewApi") @Override public boolean isForMainFrame() { return (); } @SuppressLint("NewApi") @Override public boolean hasGesture() { return (); } @SuppressLint("NewApi") @Override public String getMethod() { return (); } @SuppressLint("NewApi") @Override public Map<String, String> getRequestHeaders() { return (); } }); } } return (view, request); } @Override public WebResourceResponse shouldInterceptRequest(WebView view, String url) { if (!(url) && (url).getScheme() != null) { String scheme = (url).getScheme().trim(); if (("http") || ("https")) { return (view, injectIsParams(url)); } } return (view, url); } });
The mission is done.
Welcome to point out the problems in the code~~Learn to make progress together
Note: Pay attention to the URL-protecting Scheme, and specifically filter http and https in the code.
Introduce
There are more ways to play in the API above, such as:
- Replace WebResourceResponse and construct your own WebResourceResponse. For example, the following code replaces the network image to be requested with a local file in a package.
WebResourceResponse response = null; if (("logo")) { try { InputStream is = getAssets().open(""); response = new WebResourceResponse("image/png", "UTF-8", is); } catch (IOException e) { (); } } return response;
The WebResourceRequest interface is used in the above versions of API 21 (5.0), which can modify the requested header
@Override public Map<String, String> getRequestHeaders() { return (); }
In API 21 (5.0) or above, you can distinguish between GET requests and POST requests. In some cases, you can use them when you need to distinguish different types of requests from AJAX.
This is the article about the detailed explanation of the example of intercepting all requests and replacing URLs in WebView in Android environment. For more related Android WebView to intercept all requests and replacing URLs, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!