Andoid and javascript

  • Replies:2
  • OpenNot stickiedUnanswered
  • Forum posts: 3

Feb 18, 2018, 10:37:27 AM via Website

I am developing an application for both iOS and Android.
For iOS I am using objective-C and Xcode; for Android Java and Android Studio.
I should insert code to activate a remote javascript script, using a remote http link.

Every time this scrip is executed, a message is sent to a remote server to update statistical data.

In an htlm page this would be accoplished simply inserting a line like:

But in my case the script must be triggered from the app code.
For iOS I found the right solution: the code shown below works and statistical data are
correctly incremented when is is executed.

------------------- IOS CODE START-----------------------------------------------------------
static NSString *shinyC = @"";

  • (void)shinySend {
    result.text = @"";
    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    NSURLSession *ephemeralSession = [NSURLSession sessionWithConfiguration:sessionConfig
    delegate: self delegateQueue: [NSOperationQueue mainQueue]];
    NSURL *url = [NSURL URLWithString:shinyC];
    NSURLSessionDataTask * dataTask = [ephemeralSession dataTaskWithURL:url
    completionHandler:^(NSData *data, NSURLResponse *response, NSError *tag) {
    [self dataGot:data];
    [dataTask resume];

  • (void)dataGot: (NSData *)data {
    NSString *jscript = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    [webView evaluateJavaScript:jscript completionHandler:^(id name, NSError *error) {
    if (error == nil) result.text = @"Success";
    else result.text = [NSString stringWithFormat:@"%@",error];
    -------------------- IOS CODE END-------------------------------------------------------------

Then I tried an analog solution for Android, using the code shown below.
The shinySend method is executed in a Thread.
This apparently runs without errors, but statistical data are not incremented.
It seems that the script is not executed.
Does somebody have an idea for that ?

-------------------- ANDROID CODE START ------------------------------------------------------

static String shinyC = "//";

void shinySend() {
WebView webView;
String result;

 try {
      URL url = new URL(shinyC);
      InputStream ins = url.openStream();
      result = IOUtils.toString(ins, StandardCharsets.UTF_8);
      runOnUiThread(new Runnable() {
          public void run() {
               webView.evaluateJavascript(result, new ValueCallback<String>() {
                   public void onReceiveValue(String s) {
                         Log.e("LogName", s);
 } catch (Exception ex) {
     Log.e("error", ex.toString());

-------------------- ANDROID CODE END --------------------------------------------------------

  • Forum posts: 3

Feb 18, 2018, 12:00:49 PM via Website

Just to be clear, the remote script is correctly downloaded in both cases,
but webView.evaluateJavascript does not accomplish the desired function.

  • Forum posts: 3

Feb 18, 2018, 3:46:13 PM via Website

I realised that I have post a too complex question.
The essential point is: once you have your javascript in a string named myScript,
in iOS: [webView evaluateJavaScript:myScript completionHandler:^(id name, NSError *error)] works,
while in Android: webView.evaluateJavascript(myScript, new ValueCallback() does not.