I am working on xamarin.forms. I am creating an app for android. I have a html file in which a java script function is placed. I placed the html file inside the Assets folder. The java script function returns some value that I need to use in my c# code.
I am using a webview and inside it I load the html file. Then with help of webview.Eval() method I am trying to call the javascript method. But unfortunately this method doesn't return value. It is void type.
Then I tried to use Android.Webkit.Webview and call the webView.EvaluateJavascript(function, new JavascriptResult()); method. But I am getting null value in result.
I am using following code:
Android.Webkit.WebView webView = new Android.Webkit.WebView(GlobalClass.AndroidContext);
webView.Settings.JavaScriptEnabled = true;
webView.LoadUrl("file:///android_asset/Content/RSAGen2.htm");
webView.EvaluateJavascript(function, new JavascriptResult());
public class JavascriptResult : Java.Lang.Object, Android.Webkit.IValueCallback
{
public string Result;
public void OnReceiveValue(Java.Lang.Object result)
{
string json = ((Java.Lang.String)result).ToString();
Result = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(json);
Notify();
}
}
I am getting null value in result.
Can anyone tell me how I can get the value form javascript function with help of webview?
Regards,
Anand Dubey
Related
Hi I am new to android development,
In MainActivity.java I have webview (refer to one HTML file in loadurl eg: index1.html) and button, on clicking the button, I make a call from java to js(app.js refered in index1.html ) file where I will form an object (like map structure) and assign to a variable, then I will trigger another activity(Main2Activity.java) using Intent intent = new Intent(this, Main2Activity.class)
The Main2Activity.java also have webview (refer to one HTML file in loadurl eg: index2.html) now I have to access the variable in app1.js(index2.html) that variable was created in app.js (index1.html)
Like localstorage in js.
What changes I have to do. Thanks in advance :)
MainActivity.Java code given below:
public void ViewPdf(View view) {
WebView.loadUrl("javascript:getDetails()");
Intent intent = new Intent(this, Main2Activity.class);
startActivity(intent);
}
app.js code :
var getDetails = function(){
// the below variable I need to access in another js file that is in index2.html
details = {
date : $(".mainDate .date").val(),
no : $(".maniNo .No").val(),
shopNo: $(".shopNoIn").val(),
itemDetails : itemMap
}
}
app2.js code:
$(document).ready(function () {
// the valiable that is set in app.js()index1.html
updateBill(details);
});
I am trying to call a javascript function inside a webview. And I keep getting the following error.
[INFO:CONSOLE(1)] "Uncaught ReferenceError: onOTP is not defined", source: (1)
Please you ask, this function is only getting called inside the onPageFinished() method of the webview client. I am successfully able to call an Android function from the html but not the other way around.
Any help?
This is my javascript method:
function onOTP(otp){
console.dir("Otp Triggered " + otp);
}
This javascript code is inside a separate js file. I thought that might be the issue and added the same code to the html page with a script tag and still the same issue.
And I am calling this inside:
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mWebView.loadUrl("javascript:onOTP(465601)");
}
Can someone please let me know what I am doing wrong in this?
Thanks in advance for your help!
HtmlUnit takes lot of time to execute javascript, i would like to know if its possible to make HtmlUnit not to load javascript from url regex filters.
Not exactly, you can't only disable javascript as a whole (probably you already know it):
final WebClient webClient = new WebClient();
webClient.getOptions().setJavascriptEnable(false);
but you can use a ScriptPreProcessor the javascript, and erase what you don't want:
webClient.setScriptPreProcessor(new ScriptPreProcessor() {
#Override
public String preProcess(HtmlPage htmlPage, String sourceCode, String sourceName, int lineNumber, HtmlElement htmlElement) {
if (match...)
return "";
}
});
With version 2.3.3 from web view i am getting result string as 1364311909
But with the same 4.0 or above i am getting 1.36431e+09 a string value in different format
The Value is passing from a javascript to a webview using Web View Android Frame Work
The Webview Used With JavaScript enable :
myWebView.getSettings().setPluginState(PluginState.ON);
System.out.println("default encoding state is...."+myWebView.getSettings().getDefaultTextEncodingName());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("the Url");
JavaScript Code for retrieving the value is
public void changeIntent(final String updateId) {
showToast(updateId);
//* addToUpdatesId(updateId);
Runnable runnable = new Runnable() {
public void run() {
// your code here
Intent intent = new Intent(mContext,XXX.class);
String allId= updateId;
Global.setUpdateId(updateId);
intent.putExtra(UPDATESID, allId);
mContext.startActivity(intent);
}
};
runOnUiThread(runnable);
}
How to solve this issue. Plz let me know
We have solved it
Java Script Always take High Precision Value and it well add power of exponential at the end of the String
To make this solve Append a Empty String with the Original String That you are passing
and return a string.
like
String s= Original string+"";
return s;
Cheers......!!!!!!!!!!!
I want to create a calculator which is able to evaluate Strings like 3+(2-1)*2 or (28.4/2-1.5)+(4-2). I searched the web and found the possibility to use a WebView with JavaScript enabled which can evaluate() Strings.
How do i use JavaScript in a WebView?
How can i pass for example input.getText().toString() to the JavaScript and get back a value?
How do i access evaluate() of JavaScript in my Java-code?
I found a tutorial here. I have to load the HTML-file into my WebView, don't i?
You are going right. For using javascript in webview, after you load url to webview,(you can catch to page loading done at onPageFinish function) at onPageFinish function, you call js like this:
But first you must give permission to webview with these code blocks:
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
webview.loadUrl("javascript:setPageHeight("+contentHeight+");"); // you can pass param with call js function at loaded page.
webview.loadUrl("javascript:alert('test');"); // or you can call like this
For take result param from webview with js you must a subclass like this:
class JavaScriptInterface {
public void showToast(String msg) {
Toast.makeText(mContext, "Received Msg :" + msg,Toast.LENGTH_SHORT).show();
}
}
Then you give this to your webview at the define webview lines
webview.addJavascriptInterface(new JavaScriptInterface(), "MyAndroid");
Finally you call java function in your html in js like this:
<html>
<head>
<script type="text/javascript">
function sayHello(msg){
//calls Android's JavaScriptInterface Function
MyAndroid.showToast(msg);
}
</script>
</head>
<body >
<div onclick="sayHello('hello')"> Click Me !! </div>
</body>
</html>