How to intercept request headers in android WebView before android-21? - android

In android-21 a new callback appeared:
WebResourceResponse shouldInterceptRequest (WebView view, WebResourceRequest request)
Any chance to have them on older API, f.e. on android-19 where old callback is available?
WebResourceResponse shouldInterceptRequest (WebView view, String url)
I've tried to find invocations with headers argument (here f.e.) but i was unable to do it though they are somewhere. Any ideas?

Related

How to get all HTTP packet requests made by webview

While loading a URL in webView there are multiple of HTTP request made while loading a single page. Is there a way to track those requests? The closest I could get is to use
public boolean shouldOverrideUrlLoading(WebView view, String url)
method to view the URL requests made but could not capture the packets each page make.
I know this Question has already been asked years ago but couldn't find a solid answer that I can depend. Any help would be helpful
You can override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request)
method of WebViewClient. This will be called everytime javascript code requests any resource inside Webview.

What are the options for Android apps that uses webview after API release 26

I am an hobbyist coder mostly limited to PHP and mysql. I developed a website for one client who also asked me to present it as an android app.
So I made an hybrid app following some youtube tutorials. It worked fine mostly
but In android version 7 and version 8, webview is deprecated. they are using chrome instead.
Now can anybody please suggest me some alternative way to show my website as an app in android.
also, can I migrate myapp from webview to chorome.
webView is not Deprecated.
The Following method in webView is deprecated
Android has deprecated shouldOverrideUrlLoading(WebView view, String url) {} in WebViewClient and replaced it with it with shouldOverrideUrlLoading(WebView, WebResourceRequest), presumably for greater long-term flexibility.
instead of using
boolean shouldOverrideUrlLoading (WebView view, String url)
use
shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
create a method called initWebView in onCreate
private void initWebView() {
webView.setWebChromeClient(new MyWebChromeClient(this));
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request) {
webView.loadUrl(request.getUrl().toString());
return true;
}
}
this should do the job

Adding Header to Request in android webiew

I have an url which has to be loaded in web view, but I need to pass an header to the request.
I tried using setting headers to web view, using HashMap but it didn't worked.
I found the solutions such as web view intercepting client but did not get a proper example of how to load the url.
Can anyone suggest how to achieve it.
Try
loadUrl(String url, Map<String, String> extraHeaders)
For adding headers to resources loading requests, make custom WebViewClient and override:
API 24+:
WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request)
or
WebResourceResponse shouldInterceptRequest(WebView view, String url)

Adding custom header to all requests in shouldInterceptRequest Android webview

I want to add custom Headers to requests in the webview. I think it should be possible to do it in shouldInterceptRequest.. Since my minimum API level is less than 21 shouldInterceptRequest (final WebView view, final String url) is also called and therefore I need to add headers here as well but I am not sure how.
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
request.getRequestHeaders().put("ClientId", "ANDROID");
request.getRequestHeaders().put("Tokon", token);
}
return super.shouldInterceptRequest(view, request);
}
#Override
public WebResourceResponse shouldInterceptRequest(final WebView view, final String url) {
// I need to updated the header here
return super.shouldInterceptRequest(view, url);
}
There is a suggestion to use view.loadUrl(url,headers) but this does not work either.
The difficulty you run into with pre API 21 is the shouldInterceptRequest only provides the intercepted URL and the webview without the body of the request. I ran into this same issue and discovered the following GitHub repository
https://github.com/KeejOow/android-post-webview
The important part of this project is interceptheader.html in the assets folder. This html contains javascript that is inserted at the top of every html response the webview loads. This JS intercepts every form and ajax submission from the page and loads the body data into a java class. Next the shouldInterceptRequest method determines whether the request is POST or GET (you only get those two, unfortunately) based on whether there is any data in the body.
Finally, once it has marshalled all the relevant information, it performs the request by itself (instead of passing it off to Android), returning the resulting WebResourceResponse.
Be warned that the repository has seen some aging. I had to do some fiddling to get pages to work as I wanted them.
The best place to add your headers is in the InterceptingWebViewClient class under shouldInterceptRequest.
conn.setRequestProperty("header-name", value);

Unable to get correct URL in Android webview

In an Android web view, I'm trying to get the URL of webpages visited by the user. I'm using:
public void onPageStarted(WebView view, String url, Bitmap favicon)
public boolean shouldOverrideUrlLoading(WebView view, String url)
public void onPageFinished(WebView view, String url)
Unfortunately, for certain cases all of the above return me the wrong URL. For example when I'm on http://www.yahoo.com I get the correct URL, but once I click on any news article, the URL returned to me is still http://www.yahoo.com.
Just to confirm, I opened http://www.yahoo.com on Chrome, and then tapped the same articles to check whether Chrome gets the correct URLs. It does, which means it's possible and I'm probably missing something fundamental.
How can I get that URL in my own webview? Note that I have tried the JavaScript interface as well, but it's been of no use.

Categories

Resources