I need to know how does the shouldinterceptrequest work. I don’t know how to create and handle this method to read and replace the CSS link. Thank you!
I need to know how does the shouldinterceptrequest work. I don’t know how to
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well, the short answer is that it works quite similar to
shouldOverrideUrlLoading(WebView view, String url), as illustrated in the WebView tutorial.To get you started, see the code below. You simply override the
shouldInterceptRequest(WebView view, String url)method of your WebViewClient. Obviously you don’t have to do that inline, but for the sake of compactness that’s what I did:Catch the loading of the css file and return your own
WebResourceResponsecontaining the data you want to load in stead.Do note that this method requires API level 11.
If you want to do something similar for Android 2.x, you might want to try using the earlier mentioned
shouldOverrideUrlLoading(WebView view, String url)to avoid loading the page, fetch it manually, replace the reference to the css file with your own, and finally callloadData(String data, String mimeType, String encoding)(orloadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)) on the WebView, passing in the manipulated html content as a string.Before:
After: