1. Download our Official Android App: Forums for Android!

Apps How to get javascript value in Android

Discussion in 'Android Development' started by Divyadd, May 10, 2016.

  1. Divyadd

    Divyadd Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    May 10, 2016

    May 10, 2016
    4
    0
    5
    Female
    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.
     

    Advertisement

  2. Thom

    Thom  
    VIP Member
    Rank:
     #22
    Points:
    1,318
    Posts:
    11,945
    Joined:
    Feb 16, 2011

    Feb 16, 2011
    11,945
    4,920
    1,318
    Male
    Software Developer
    Keene, NH USA
    Welcome to Android Forums.

    This thread was moved to this forum that is dedicated to development.

    ... Thom
     
    Jimmy lay likes this.
  3. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,286
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,286
    10,384
    1,988
    Male
    Software developer
    South West of England
    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?
     
    Jimmy lay likes this.
  4. Divyadd

    Divyadd Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    May 10, 2016

    May 10, 2016
    4
    0
    5
    Female
    <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />
     
  5. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,286
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,286
    10,384
    1,988
    Male
    Software developer
    South West of England
    And what is your target deployment environment running?
     
    Jimmy lay likes this.
  6. Divyadd

    Divyadd Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    May 10, 2016

    May 10, 2016
    4
    0
    5
    Female
    This is my emulator AVD. Is this you asked? Pls lemme know if any other.

    Target Name : Android 4.0
    Platform : 4.0
    API Level : 14
    CPU/ABI : ARM(armeabi-v7a)
     
  7. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,286
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,286
    10,384
    1,988
    Male
    Software developer
    South West of England
    Well that's your problem. As I said, it's not possible to run this app on a system with API < 19
     
  8. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,286
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,286
    10,384
    1,988
    Male
    Software developer
    South West of England
    To make your app work with older API versions, you could use the following code

    Code (Text):
    1.  
    2. private void executeJavascript(WebView view,String javascript) {
    3.     if(android.os.Build.VERSION.SDK_INT >=19) {
    4.         webView.evaluateJavascript(js,null);
    5.     }
    6.     else {
    7.         webView.loadUrl("javascript: "+ js);
    8.     }
    9. }
    10.  
     
    Divyadd likes this.
  9. Divyadd

    Divyadd Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    May 10, 2016

    May 10, 2016
    4
    0
    5
    Female
    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.
     
  10. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,286
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,286
    10,384
    1,988
    Male
    Software developer
    South West of England

Share This Page

Loading...