• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to get javascript value in Android

Divyadd

Lurker
May 10, 2016
4
0
I am very new to Android and developing my first app. My Version is 6.0 and API veresion is 21.

My app is having a textbox and a button in its home page. I am putting my url and hit submit button so that a webview will loads this url.

In my url I included some javascript functions, they are defined to retuning some values.

I am trying to get that value and assign the value to other textbox.

I tried in two ways but none of them worked for me.

First
I created a new class as follows,

public class JsInterface {
private String val;

@JavascriptInterface
void receiveString(String value) {
Log.d("This is what from JS-", value);
val = value;
}
String Val() {
return val;
}

}

My JS function

function goAndroid(Go Value to Script){
var goString = "Hello " + Go Value to Script;
window.MyAndroidApp.receiveString(goString);
}

In mainActivity.java

WebView.addJavascriptInterface(new JsInterface(), "MyAndroidApp");
WebView.loadUrl("javascript:goAndroid('Go Value to Script')")
//Below value is null..
System.out.println(new JsInterface().Val());

Here the system out comes nothing...

Second
As I am using API version 21, I followed the another way,

webView.evaluateJavascript("(function() { return goAndroid('My Value'); })();", new ValueCallback() {
@override
public void onReceiveValue(String s) {
Log.d("LogName--------------", s);
}
});

Here I am getting a small popup which says " unfortunately, app has stopped". In LogCat I am getting the following error

05-10 17:36:13.922: E/AndroidRuntime(822): FATAL EXCEPTION: main 05-10 17:36:13.922: E/AndroidRuntime(822): java.lang.NoSuchMethodError: android.webkit.WebView.evaluateJavascript 05-10 17:36:13.922: E/AndroidRuntime(822): at com.example.asdf.MainActivity$5.onClick(MainActivity.java:209)

Thanks in advance.
 
What version of Android is your app deployed to? Because it looks like the runtime knows nothing about method evaluateJavascript().
This method was introduced in API level 19. Before that it didn't exist.
So if you are compiling at API 21, but deploying to an earlier version than 19, then you would get this error.
What is your minimum API version?
 
  • Like
Reactions: Jimmy lay
Upvote 0
To make your app work with older API versions, you could use the following code

Code:
private void executeJavascript(WebView view,String javascript) {
    if(android.os.Build.VERSION.SDK_INT >=19) {
        webView.evaluateJavascript(js,null);
    }
    else {
        webView.loadUrl("javascript: "+ js);
    }
}
 
  • Like
Reactions: Divyadd
Upvote 0
Thanks for your Answwer..

I am able to call this webView.loadUrl("javascript: "+ js); without issues. I tested a following function

function goAndroid( value){
alert(value);
}

This shows alert in android..

But how do I get the return value for the following function

function goAndroid( value){
var goString = "Hello " + value;
window.MyAndroidApp.receiveString(goString);
// return goString;
}

I want to have this return value in my mainActivity as string. Also I am able to make alert as follows

webView.loadUrl("javascript:alert(goAndroid(myValue));

I want to get this return value as a string and show in a textbox.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones