Is there any way to get the currently successfully loaded and not loaded files from URL into android webview. That is, if I am loading the following,
1.js, 2.js, 3.js
1.css, 2.css, 3.css
in html page and load the html file into android webview.
I need to list the loaded files in webview and also which are unable to load into android webview. I tried using the onConsoleMessage() from WebchromeClient which detects only the console error alone. This method not getting the error messages for css image.
Is this possible to list the files from URL?
You can get the list of resources the WebView is trying to load by overriding WebViewClient.shouldInterceptRequest like so:
class MyWebViewClient extends WebViewClient {
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
android.util.Log.i("MyWebViewClient", "attempting to load resource: " + url);
return null;
}
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
android.util.Log.i("MyWebViewClient", "attempting to load resource: " + request.getUrl());
return null;
}
}
Remember that shouldInterceptRequest is called on a background thread and so you need synchronize access to any shared data structures.
There is no Java API to find out whether the WebView has successfully loaded a given resource though.
Related
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.
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);
I am using WebView to render local HTML pages loaded from a string, and for security reasons need to block loading any external resources, but need to notify the user if anything was actually blocked and give the user an option to fully load the page with remote images/scripts.
I'm initially blocking network resources with webView.getSettings().setBlockNetworkLoads(true). This works well. Next I need to determine if the loaded HTML content actually contained any references to external networked resources that were blocked. Can anyone please tell me how to do that?
I would like this to work with API 8
You need to do the following:
class MyWebViewClient extends WebViewClient {
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
// This method will get called when resources are to be loaded
return new WebResourceResponse(null, null, new ByteArrayInputStream(new byte[0]));
}
}
Then assign this client to your webview and handle resource loading in the above method.
This way you should be able to disable loading external resources without using the setBlockNetworkLoads(true) method and get the callback at the same time.
I have an App that uses custom schemes in Android WebViewClient's shouldInterceptRequest(WebView view, String url) and shouldOverrideUrlLoading(WebView view, String url) to intercept requests in a web application and use a native library to fetch resources from elsewhere in shouldInterceptRequest. This has worked fine up until Android 4.4 KitKat, where Google has made some crucial changes to the webView component.
http://developer.android.com/guide/webapps/migrating.html#URLs
Now the url received in shouldOverrideUrlLoading suddenly gets invalid, looking like this; custom-scheme:////my.pathname.com/. First I suspected the extra slashes were because Android did not think the url were valid RFC3986, but in a series of resource fetches (css, js, images), the url starts off correct and suddenly changes to the invalid format. The webView in Android 4.3 kept the url correctly as custom-scheme://my.pathname.com/. It seems like the base url suddenly changes to '/' instead of 'my.pathname.com'.
Then my attention changed to the fact that the webView 4.4 migration guide talks about:
If you call methods on WebView from any thread other than your app's UI thread, it can cause unexpected results. http://developer.android.com/guide/webapps/migrating.html#Threads
This also might be what I am experiencing, but I have not yet come up with a solution where I can use runOnUiThread() to fetch data with the native api and return it to the webView inside shouldInterceptRequest. Has anyone experienced something similar?
Here is a simplified version of my shouldInterceptRequest code:
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
if (urlStartsWithKnownPrefix(url)) {
UrlFetchResult fetchRes = api.fetchUrl(url);
String charset = "utf-8";
String mime = fetchRes.getMimetype();
WebResourceResponse res = new WebResourceResponse(mime, charset, new ByteArrayInputStream(fetchRes.getResult()));
return res;
}
return null;
}
Are you using jquery-mobile by any chance? This sounds very similar to: How can I use relative urls in ajax requests within trigger.io apps on Android 4.4 (kitkat)?
I am working on one Android App where I am loading local file contents into webview using following syntax.
url = "file://" + mLocalFilePath + java.io.File.separator + CURRENT_FILE;
webView.loadUrl(url);
The issue is the webview loads file contents and also loads the various images from url present in webpage ( webpage has many images ).
The challenge I am facing is I want to get notified when webview finish rendering entire html page.I want to get notified when webpage is 100% loaded.
I reffered various forums and also tried using
window.onload function
as well as
public void onPageFinished(WebView view, String url)
But I did not get desire output,I get callback before html page is completely loaded.I am looking for way so that I will get notified only when webpage has finished rendering all the images present in web page.( i.e Webpage is 100% loaded.)
I am not looking for complete answer even hints will be appreciated.
Thanks
Try this way
webView.setWebChromeClient(new WebChromeClient(){
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if(newProgress==100){
// page loading completes
}
}
});
I used deprecated PictureListener that start when all images on the pages are loaded. It deprecated and you need at least one image in the html but if you can manage this (you could place transparent 1px*1px image) it's a good solution.
wv.setPictureListener(new PictureListener() {
#Override
public void onNewPicture(WebView view, Picture picture) {
// some code here
}
}
});