Android WebView with Javascript calls behaving oddly - java

I understand how to call Javascript from my WebView, that isn't the issue.
if(javascriptCall!=null){
String hardCodedexample = "1959602";
webView.loadUrl("http://myappexample+ username);
webView.loadUrl("javascript:" + "window.application.relocateTo({location:'patients/details',params:{EMPIID:"+ hardCodedEMPIID +"}})");
}
javascriptCall is a String which is passed to my webview activity. hardCodedEMPIID is just an example for our testing. My problem is that when I allow the app the run normally on the device my javascript method doesn't seem to work. It loads the myappexample and I can't tell what happened with my javascript call.
When I debug the app and single step through the code it properly calls the method. I'm confused on how this is possible? the different between single step and normal execution.

You should wait for onPageFinished(WebView view, String url) event called by WebViewClient (http://developer.android.com/reference/android/webkit/WebViewClient.html#onPageFinished(android.webkit.WebView, java.lang.String) ) . JavaScript file with your function might not be loaded right away after calling loadUrl.

Related

webpage takes a lot of time to load

In a webview in Android, I first load a webpage and then display only a part of it using some javascript commands. During the whole process a "loading" message is displayed.
Problem : It take s alot of time to load even if the internet speed is fast. (>60 sec always). How to reduce time ?
This is my WebViewClient class that I attach with the webview (and the class contains only 1 method):
#Override
public void onPageFinished(WebView view, String url){
view.loadUrl("javascript:document.getElementsByClassName('menu')[0].innerHTML=''");
view.loadUrl("javascript:document.getElementsByClassName('gbh')[0].innerHTML=''");
view.loadUrl("javascript:document.getElementsByClassName('input')[0].innerHTML=''");
view.loadUrl("javascript:document.getElementsByClassName('card_title')[0].innerHTML=''");
view.loadUrl("javascript:document.getElementsByClassName('cell_input')[0].innerHTML=''");
view.loadUrl("javascript:document.getElementsByName('pre')[0].innerHTML=''");
view.loadUrl("javascript:document.getElementById('footer').innerHTML=''");
view.loadUrl("javascript:document.getElementsByClassName('foot')[0].innerHTML=''");
}
Sir, You could use the YSlow or Google PageLoad plugins for your browser, to get specific tips on how to improve page load and speed up your page.
By default, a WebView provides no browser-like widgets, it does not enable JavaScript and also web page errors are ignored.

Hard Refresh in GWT

Using Google Web Toolkit, I'd like to code the equivalent of a hard refresh (control + F5).
I don't believe (or know) if GWT's Window.Location will work.
import com.google.gwt.user.client.Window.Location;
Window.Location = currentPage; // I don't think it'll be hard refresh
For reloading the current page you need to call Window.Location.reload() method.
Reloads the current browser window. All GWT state will be lost.
Or you can even specify your own JSNI (below how todo), because by default force reload is false :
public static native void forceReload() /*-{
$wnd.location.reload(true);
}-*/;
According to https://developer.mozilla.org/en-US/docs/DOM/window.location#Methods you would need to call window.location.reload(true) to force the reload of the current page.
Unfortunately GWT wraps only the window.location.reload() via Window.Location.reload(), and it is up to the browser to retrieve the page from the cache or from another get. This is done to achieve the most cross-browser solution.
Never tried but you should be able to use the following.
public static native void reload(boolean force) /*-{
$wnd.location.reload(force);
}-*/;
For reload gwt page, you have two options:
1) Window.Location.reload();
Reloads the current browser window. All GWT state will be lost.
2) Window.Location.replace("newurl");
Replaces the current URL with a new one. All GWT state will be lost. In the browser's history, the current URL will be replaced by the new URL.

Android Webview, working with Anchors

I am loading a webpage into a webview from the database with the loadDataWithBaseURL() function and am trying to subsequently jump to an Anchor point. When I place a button just below the webview that uses the loadUrl()-function, it jumps to the Anchor point just fine. But if I place the loadUrl() after the loadDataWithBaseURL() it says page not found.
webview.loadDataWithBaseURL("app:myhtml", data, "text/html", "UTF-8", null);
webview.loadUrl("app:myhtml#tips");
I assumed it was because the load had not completed, and thus it couldn't find the Anchor, but I tried a loop to wait for it to getProgress() to equal 100, and have verified it's reaching 100 before that command executes, but it still won't find the page unless it's associated with the button click.
Any Ideas out there on how I can load a page from the database and then jump to an anchor point at the same time. I'm working with the Android 2.1 SDK.
Thanks to your help I got it to load directly to my anchor by adding this little bit of code. However I can't scroll around on the page, it starts to move and then snaps back. I suspect because it is reloading the page to that anchor each time. I'm fairly new to Android, and Java even, so this may be the wrong implementation of your suggestion, but I'd certainly appreciate a little more direction. Here is the code I used:
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(webview, "app://data");
webview.loadUrl("app://data#tips");
}
});
It seems this is making an infinite loop.
Try using valid URL as a base URL in loadDataWithBaseURL(), such as app://myhtml.
Also, to make sure the data was loaded implement WebViewCLient.onPageFinished().
It was getting in an infinite onPageFinished loop before. It had nothing to do with me scrolling, it was just continuously loading the same page. This code allows it to load without without getting stuck in a loop.
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if(url.contains("#") != true){
webview.loadUrl("app://data#tips");
}
}
});

android: call javascript from java problem

I want to know if there is way to call javascript from java on android?
In my program, I interact java and javascript together. I am using java to receive response(json data) from TCP server and save them into a file. In webview I am using javascript jQuery getJSON() function to retrieve that file and using jQuery plot chart library to draw chart. Now, there is no relationship between java and javascript. Every time when I update data and file, I still need to click a button in webview to trigger the draw function. I want the programmes to be smart and handy. Is that a way to call or execute javascript from java. I know one way:
Button update = (Button)findViewById(R.id.update);
update.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
wv.loadUrl("javascript:document.write('hello')");
}
});
But the problem is I already do a index page by loadurl().
final WebView wv = (WebView) findViewById(R.id.webkankan);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("file:///android_asset/index.html");
When I trigger this click event, all contents were gone only a string "hello" there. Another thing is why I need to change webview's type to final to avoid eclipse error. Does this is the problem to trigger my main problem? If so, how can I fix it?
Thanks for you patience.
Cheers!
For instance you have a javascript method in the index.html called loadData() which reads the file you saved in the java, then what you can do is wv.loadUrl("javascript:loadData()");. This actually call the javascript method and you can then read the file in that method. Hope this solves your problem.
or in simple terms. jus do this webView.loadUrl("javascript:jsmethodname()"); to execute javascript from java.
You can try to communicate java with javascript registering a java object to the webview that is executing the javascript.
The method addJavascriptInterface from Webview will allow you to make available a Java object to the Javascript scope, something like this:
WebView mWebView = new WebView(mContext);
//... webview initialization, js enabling etc.
MyProxyObject obj = new MyProxyObject(); //This object can interchange just basic types, but Strings are basic types
mWebView.addJavascriptInterface(obj,"myproxyobj");
With that code what you will have in the Javascript context you will have an object 'myproxyobj' that is actually a Java object.
Remember, you can interchange just basic types.
For more info check the following url:
http://developer.android.com/guide/webapps/webview.html
Specially check the section: Binding JavaScript code to Android code
Cheers,
Francisco

How to clear the (CSS) visited history of an Android WebView?

I try to reuse an existing WebView by clearing any private data the previous user left behind:
CookieManager.getInstance().removeAllCookie();
webview.clearHistory();
webview.clearFormData();
webview.clearCache(true);
clearHistory seems only to clear the back/forward list, accessible via API, but not the internal list used for coloring links inside the web content.
I even tried the following, suggested by another stackoverflow answer:
deleteDatabase("webview.db");
deleteDatabase("webviewCache.db");
I still have no luck: CSS :visited selectors still work after reloading the page.
An alternative would be to use the API level 11 private browsing feature (new constructor argument), but then I cannot benefit from visited links at all; and can no longer target older versions.
Maybe someone has a solution for this issue? Thanks for your help.
Summary of the answers I got so far:
I tried these two answers, but the first seems to clear HTML5 data storage and the latter seems to be specific to the built-in browser:
WebStorage.getInstance().deleteAllData();
Browser.clearHistory(getContentResolver());
WebChromeClient.getVisitedHistory(ValueCallback<String[]> callback) is only called after the first time I create a new WebView in a recently installed application.
I tried to remove the WebView from view hierachy and create a new one, but unfortunately the visited history seems to be stored for the whole application.
Override WebChromeClient and WebViewClient... Damn that was hidden.
I actually had to dig up a bit to find this out.
WebView webView = (WebView)findViewById(R.id.myWebView);
WebChromeClient myWebChromeClient = new WebChromeClient(){
#Override
public void getVisitedHistory(ValueCallback<String[]> callback) {
// called during webview initialization, original implementation does strictly nothing
// and defaults to the native method WebViewCore.nativeProvideVisitedHistory()
String[] myUserHistory = getVisitedUrlsFromMyOwnDatabase(userId);
callback.onReceiveValue(myUserHistory);
}
};
WebViewClient myWebViewClient = new WebViewClient(){
#Override
public void doUpdateVisitedHistory(WebView view, String url,
boolean isReload) {
// called whenever there is a new link being visited
insertIfNotExistVisitedUrlIntoMyOwnDatabaseForUser(userId);
super(view, url, isReload);
}
}
webView.setWebViewClient(myWebViewClient);
webView.setChromeClient(myWebChromeClient);
webView.getSettings().etc(whatever)...
I think I'm "almost there". Here's the part I managed: what it does so far is remove css history altogether, so we're halfway there. I can't get the browser to recognize the url format I'm providing in "myUserHistory", so in effect the only feature this code does is reset css history altogether, but it's only called once when the WebView is instanciated (or created, didn't check), so for a true multiuser experience you'd need to recreate the webview at each login.
My problem now is that I can't manage to load the urlHistory properly. My Honeycomb Xoom webview seems to ignore my data.
Ah well, I hope it works for you. For me just calling callback.onReceiveValue(new String[]{}); in getVisitedHistory() will be good enough.
EDIT:
I just put twenty more minutes into it because I'm curious. This method is what delegates to the WebChromeClient (mCallbackProxy = WebChromeClient).
protected void populateVisitedLinks() {
ValueCallback callback = new ValueCallback<String[]>() {
public void onReceiveValue(String[] value) {
sendMessage(EventHub.POPULATE_VISITED_LINKS, (Object)value);
}
};
mCallbackProxy.getVisitedHistory(callback);
}
It's protected in WebViewCore, which is a private attribute of WebView with no accessor. The sendMessage delegates to EventHub which is private, and WebViewCore is filled with private native methods, and one of these seems to be the one actually calling the populateVisitedLinks() method during the initialization.
Unless someone at Google adds a public method to WebView to trigger the repopulation, I'm afraid it's practically impossible to achieve your goal. Sorry :(
As a side note, all these native visited history handling really makes me wonder: why do hardware manufacturers care so much about which urls we visited? ;) <<< sarcasm
As an alternate solution, you could try adding your own CSS with the same base colors the default CSS has and switch the CSS by another one (with same color for both "types" of links) when you want to reset the visited links.
A:link{color: "#990000"; text-decoration: none;}
A:visited{color: "#990000"; text-decoration: none;}
A:hover{color: "#ff0000"; text-decoration: none;}
If you can obtain a Browser instance (maybe you can set a WebChromeClient to WebView) you can use its clearHistory() method.
Does WebStorage.clearAllData() have the desired effect? Unfortunately, the documentation on this class is very sparse compared to WebView and doesn't say whether it applies to WebViews.
The exact time you're calling clearHistory() may also have an effect. Clearing it and then navigating to a new page may still keep the first page in history, and you have to call the method after the new page has loaded.
Personally, if privacy is a real issue, I would create a new set of objects from scratch for this new session if possible.

Categories