1

This question isn't new here, but still I cannot get it where I go wrong. I just want to evaluate some javascript.

My simple code looks like:

WebView wv = new WebView(context);
WebSettings settings = wv.getSettings();
settings.setJavaScriptEnabled(true);

JavaBridgeToJS bridgeToJS = new JavaBridgeToJS();
wv.addJavascriptInterface(bridgeToJS, "javaCallback");

String src = "javascript: var result= 3 + 3; window.javaCallback.resultOfAdd(result)";

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
        Log.d(TAG, "Newer than JellyBean.");
        ValueCallback<String> result = new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String value) {
                Log.d(TAG, "Got in onReceiveValue: " + value);
            }
        };
        wv.evaluateJavascript(src, result);
    }else {
        Log.d(TAG, "Older than Kitkat");
        wv.loadUrl(src);
    }

and

public class JavaBridgeToJS {
    @JavascriptInterface
    public void resultOfAdd(String result) {
        Log.d(TAG, "Here is: " + result);
    }
}

It is working good for API 19+, even without if with checking of version (I mean that it works with loadUrl as well as with evaluateJavascript)

But when I try API 18, the callback in java is not called. I have looked around (remove line break, or quotes missing (possibly infected, watched on phone), and so on...)

So far I have no luck. Any idea?


EDIT

Well, I have added code for handling errors inside of JS in case, that I have missed something.

wv.setWebChromeClient(new CustomWebChromeClient());

as

public class CustomWebChromeClient extends WebChromeClient {
    private CustomWebChromeClient() {
        super();
    }

    @Override
    public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
        UILog.d(TAG, consoleMessage.message());
        return super.onConsoleMessage(consoleMessage);
    }
}

and I was suprised with:

E/dalvikvm: Could not find class 'android.webkit.PermissionRequest', referenced from method com.example.WebViewDoll$CustomWebChromeClient.access$super
VFY: unable to resolve check-cast 1896 (Landroid/webkit/PermissionRequest;) in Lcom/example/WebViewDoll$CustomWebChromeClient;
E/dalvikvm: Could not find class 'android.webkit.WebChromeClient$FileChooserParams', referenced from method com.example.WebViewDoll$CustomWebChromeClient.access$super
VFY: unable to resolve check-cast 1899 (Landroid/webkit/WebChromeClient$FileChooserParams;) in Lcom/example/WebViewDoll$CustomWebChromeClient;
E/dalvikvm: Could not find class 'android.webkit.PermissionRequest', referenced from method com.example.WebViewDoll$CustomWebChromeClient.access$super

D/WebViewDoll: Uncaught TypeError: Cannot call method 'resultOfAdd' of undefined
E/Web Console: Uncaught TypeError: Cannot call method 'resultOfAdd' of undefined at null:1
1
  • UPDATE I have made new project, just in case, but still nothing. I suppose this should work github repo Commented Jun 1, 2016 at 7:07

1 Answer 1

0

I did not find an answer why this is not working, but with help of this using js in webview I was able to make a workaround ...

I have to inject my js into a html page. So my wrapper arround js looks like:

String src = "<html><head><script type=\"text/javascript\">" +
            "function myFunction()" +
            "{" +
            "    javaCallback.resultOfAdd(3+3);" +
            "}" +
            "</script></head><body onload=\"myFunction()\"/>" +
            "</html>";

and instead of wv.loadUrl I have used:

wv.loadData(src, "text/html", "UTF-8"); 

This combination have same result as needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.