WebResourceRequest provided in shouldInterceptRequest(WebView view, WebResourceRequest request) does not contain request body information, the SDK can only successfully intercept GET requests, but not POST requests.WebSettings webSettings = mWebView.getSettings();// Adopt the default cache policy, i.e., the cache will always be used before it expireswebSettings.setCacheMode(WebSettings.LOAD_DEFAULT);// Load web image resourceswebSettings.setBlockNetworkImage(false);// Support JavaScript scriptswebSettings.setJavaScriptEnabled(true);// Support zoomwebSettings.setSupportZoom(true);mWebView.setWebViewClient(new WebViewClient() {// Use this method for API 21 or later@SuppressLint("NewApi")@Overridepublic WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {if (request != null && request.getUrl() != null && request.getMethod().equalsIgnoreCase("get")) {String scheme = request.getUrl().getScheme().trim();String url = request.getUrl().toString();Log.d(TAG, "url:" + url);// HTTPDNS resolves the network and image requests of CSS filesif ((scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))&& (url.contains(".css") || url.endsWith(".png") || url.endsWith(".jpg") || url .endsWith(".gif"))) {try {URL oldUrl = new URL(url);URLConnection connection = oldUrl.openConnection();// Get the HTTPDNS query resultString ips = MSDKDnsResolver.getInstance().getAddrByName(oldUrl.getHost());String[] ipArr = ips.split(";");if (2 == ipArr.length && !"0".equals(ipArr[0])) { // Get the IP successfully through HTTPDNS, replace the URL, and set the host headerString ip = ipArr[0];String newUrl = url.replaceFirst(oldUrl.getHost(), ip);connection = (HttpURLConnection) new URL(newUrl).openConnection(); // Set the host field of the HTTP request headerconnection.setRequestProperty("Host", oldUrl.getHost());}Log.d(TAG, "contentType:" + connection.getContentType());return new WebResourceResponse("text/css", "UTF-8", connection.getInputStream());} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}return null;}// Use this method for API 11–20public WebResourceResponse shouldInterceptRequest(WebView view, String url) {if (!TextUtils.isEmpty(url) && Uri.parse(url).getScheme() != null) {String scheme = Uri.parse(url).getScheme().trim();Log.d(TAG, "url:" + url);// HTTPDNS resolves the network and image requests of CSS filesif ((scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))&& (url.contains(".css") || url.endsWith(".png") || url.endsWith(".jpg") || url.endsWith(".gif"))) {try {URL oldUrl = new URL(url);URLConnection connection = oldUrl.openConnection();// Get the HTTPDNS query resultString ips = MSDKDnsResolver.getInstance().getAddrByName(oldUrl.getHost());String[] ipArr = ips.split(";");if (2 == ipArr.length && !"0".equals(ipArr[0])) { // Get the IP successfully through HTTPDNS, replace the URL, and set the host headerString ip = ipArr[0];String newUrl = url.replaceFirst(oldUrl.getHost(), ip);connection = (HttpURLConnection) new URL(newUrl).openConnection(); // Set the host field of the HTTP request headerconnection.setRequestProperty("Host", oldUrl.getHost());}Log.d(TAG, "contentType:" + connection.getContentType());return new WebResourceResponse("text/css", "UTF-8", connection.getInputStream());} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {}}}return null;}});// Load web resourcesmWebView.loadUrl(targetUrl);
Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback